41 #if !defined(_WIN32) && !defined(_WIN64) 44 #define GETTIMEOFDAY 1 46 #define snprintf _snprintf 49 #if defined(GETTIMEOFDAY) 52 #include <sys/timeb.h> 55 #if !defined(_WIN32) && !defined(_WIN64) 59 #define _unlink unlink 64 #define min(A,B) ( (A) < (B) ? (A):(B)) 74 #define MAX_FUNCTION_NAME_LENGTH 256 78 #if defined(GETTIMEOFDAY) 112 static FILE* Log_destToFile(
const char *dest);
113 static int Log_compareEntries(
const char *entry1,
const char *entry2);
117 #if defined(GETTIMEOFDAY) 124 #if defined(_WIN32) || defined(_WIN64) 136 #if !defined(_WIN32) && !defined(_WIN64) 144 if ((envval = getenv(
"MQTT_C_CLIENT_TRACE")) != NULL && strlen(envval) > 0)
146 if (strcmp(envval,
"ON") == 0 || (
trace_destination = fopen(envval,
"w")) == NULL)
165 if ((envval = getenv(
"MQTT_C_CLIENT_TRACE_MAX_LINES")) != NULL && strlen(envval) > 0)
171 if ((envval = getenv(
"MQTT_C_CLIENT_TRACE_LEVEL")) != NULL && strlen(envval) > 0)
173 if (strcmp(envval,
"MAXIMUM") == 0 || strcmp(envval,
"TRACE_MAXIMUM") == 0)
175 else if (strcmp(envval,
"MEDIUM") == 0 || strcmp(envval,
"TRACE_MEDIUM") == 0)
177 else if (strcmp(envval,
"MINIMUM") == 0 || strcmp(envval,
"TRACE_MINIMUM") == 0)
179 else if (strcmp(envval,
"PROTOCOL") == 0 || strcmp(envval,
"TRACE_PROTOCOL") == 0)
181 else if (strcmp(envval,
"ERROR") == 0 || strcmp(envval,
"TRACE_ERROR") == 0)
195 #if !defined(_WIN32) && !defined(_WIN64) 196 if (stat(
"/proc/version", &buf) != -1)
200 if ((vfile = fopen(
"/proc/version",
"r")) != NULL)
204 strcpy(
msg_buf,
"/proc/version: ");
265 #if defined(GETTIMEOFDAY) 266 gettimeofday(&now_ts, NULL);
267 if (now_ts.tv_sec !=
last_ts.tv_sec || now_ts.tv_usec !=
last_ts.tv_usec)
270 if (now_ts.time !=
last_ts.time || now_ts.millitm !=
last_ts.millitm)
282 if (new_trace_queue == NULL)
286 trace_queue = new_trace_queue;
316 #if defined(GETTIMEOFDAY) 317 timeinfo =
localtime((time_t *)&cur_entry->
ts.tv_sec);
322 #if defined(GETTIMEOFDAY) 323 sprintf(&
msg_buf[22],
".%.3lu ", cur_entry->
ts.tv_usec / 1000L);
325 sprintf(&
msg_buf[22],
".%.3hu ", cur_entry->
ts.millitm);
332 if (cur_entry->
has_rc == 2)
337 if (cur_entry->
has_rc == 1)
370 (*trace_callback)(log_level, msg);
392 if (trace_queue == NULL)
397 memcpy(&(cur_entry->
ts), &now_ts,
sizeof(now_ts));
401 strncpy(cur_entry->
name, buf,
sizeof(cur_entry->
name));
421 const char *temp = NULL;
426 if (format == NULL && (temp =
Messages_get(msgno, log_level)) != NULL)
429 va_start(args, format);
452 if (trace_queue == NULL)
461 memcpy(&(cur_entry->
ts), &now_ts,
sizeof(now_ts));
463 cur_entry->
number = msgno;
465 cur_entry->
depth = current_depth;
466 strcpy(cur_entry->
name, name);
467 cur_entry->
level = log_level;
468 cur_entry->
line = line;
483 static FILE* Log_destToFile(
const char *dest)
487 if (strcmp(dest,
"stdout") == 0)
489 else if (strcmp(dest,
"stderr") == 0)
493 if (strstr(dest,
"FFDC"))
494 file = fopen(dest,
"ab");
496 file = fopen(dest,
"wb");
502 static int Log_compareEntries(
const char *entry1,
const char *entry2)
504 int comp = strncmp(&entry1[7], &entry2[7], 19);
508 comp = strncmp(&entry1[1], &entry2[1], 4);
518 int Log_dumpTrace(
char* dest)
522 const int msgstart = 7;
524 int trace_queue_index = 0;
526 if ((file = Log_destToFile(dest)) == NULL)
528 Log(
LOG_ERROR, 9, NULL,
"trace", dest,
"trace entries");
532 fprintf(file,
"=========== Start of trace dump ==========\n");
536 if (trace_queue_index == -1)
542 trace_queue_index = 0;
544 while (cur_trace_entry || trace_queue_index !=
next_index)
546 if (cur_trace_entry && trace_queue_index != -1)
548 if (Log_compareEntries((
char*)cur_trace_entry->
content,
msg_buf) > 0)
549 cur_trace_entry = NULL;
554 fprintf(file,
"%s\n", &((
char*)(cur_trace_entry->
content))[msgstart]);
559 fprintf(file,
"%s\n", &
msg_buf[7]);
564 trace_queue_index = 0;
568 fprintf(file,
"========== End of trace dump ==========\n\n");
569 if (file != stdout && file != stderr && file != NULL)
static Log_traceCallback * trace_callback
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
static int sametime_count
static void Log_trace(enum LOG_LEVELS log_level, const char *buf)
void Log_setTraceLevel(enum LOG_LEVELS level)
static char * trace_destination_name
static int trace_queue_size
int Thread_lock_mutex(mutex_type mutex)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
static void Log_output(enum LOG_LEVELS log_level, const char *msg)
static traceEntry * trace_queue
std::tm localtime(std::time_t time)
char name[MAX_FUNCTION_NAME_LENGTH+1]
void Log_setTraceCallback(Log_traceCallback *callback)
static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry *cur_entry)
ListElement * ListNextElement(List *aList, ListElement **pos)
static mutex_type log_mutex
int Thread_unlock_mutex(mutex_type mutex)
void Log(enum LOG_LEVELS log_level, int msgno, const char *format,...)
static char * Log_formatTraceEntry(traceEntry *cur_entry)
enum LOG_LEVELS trace_level
void Log_stackTrace(enum LOG_LEVELS log_level, int msgno, int thread_id, int current_depth, const char *name, int line, int *rc)
static enum LOG_LEVELS trace_output_level
const char * Messages_get(int index, enum LOG_LEVELS log_level)
struct timeval now_ts last_ts
int Log_initialize(Log_nameValue *info)
#define MAX_FUNCTION_NAME_LENGTH
void Log_traceCallback(enum LOG_LEVELS level, const char *message)
static traceEntry * Log_pretrace(void)
static FILE * trace_destination
static char * trace_destination_backup_name
static pthread_mutex_t log_mutex_store
static int max_lines_per_file
trace_settings_type trace_settings