static char* log_bake(const char* msg, enum log_type type) { /* Convert type to string */ char* type_str = log_type_to_string(type); /* Get lengths */ size_t msg_length = strlen(msg); size_t type_length = strlen(type_str); /* Calculate total output string length */ size_t length = type_length + 2 + msg_length; /* Allocate space for log */ char* log = (char*) malloc(length); memset(log, 0, length); /* Construct log */ strncpy(log, type_str, type_length); strncat(log, " ", 1); strncat(log, msg, msg_length); return log; }
//============================================================================== void log_add(int log_type, const char *message) { #ifndef DEMS_DEVICE pthread_mutex_lock(&mutex_log); #endif #ifndef DEBUG_MODE if(log_type == LOG_DEBUG) goto exit; #endif #ifndef USE_EXTRA_LOGS if(log_type == LOG_EXTRA) goto exit; #endif #ifndef USE_WAIT_LOGS if(log_type == LOG_WAIT) goto exit; #endif const char *log_type_str = log_type_to_string(log_type); if( (log_type == LOG_INFO) || (log_type == LOG_CMD) || (log_type == LOG_NO_IDENT) #ifndef SILENT_MODE || (log_type >= LOG_ERROR_CRITICAL) #endif ) printf("%s%s\n", log_type_str, message); char full_file_name[256]; char file_name[64]; log_gen_name(LOG_NAME_DATE_S, log_prefix, file_name); sprintf(full_file_name, "%s/%s", log_path, file_name); make_dir(log_path); if(log_enable) { FILE *log = fopen(full_file_name, "a"); if(log != NULL) { char time_str[16]; get_cur_time_str(time_str, LOG_LONG_FORMAT); fprintf(log, "%s %s%s\n", time_str, log_type_str, message); fclose(log); } else printf("[WARNING] Can not open log file, %s\n", full_file_name); } exit: #ifndef DEMS_DEVICE pthread_mutex_unlock(&mutex_log); #endif return; }