diff options
Diffstat (limited to 'src/aklog.c')
-rw-r--r-- | src/aklog.c | 78 |
1 files changed, 52 insertions, 26 deletions
diff --git a/src/aklog.c b/src/aklog.c index 05166c7..d453353 100644 --- a/src/aklog.c +++ b/src/aklog.c @@ -13,6 +13,10 @@ void ak_log_print_log_line(char* line) int i = 0; int spaces_found = 0; int last_space = -1; + long int l = 1000000000; + long int ts = 0; + struct tm *timeInfo; + char ts_string[16]; // %Y%Y%Y%Y%m%m%d%d_%H%H%M%M%S%S while ( line[i] != '\0' ) { if ( line[i] == ' ' ) // && spaces_found < 4) @@ -29,12 +33,47 @@ void ak_log_print_log_line(char* line) { if ( line[k] == ' ' ) { - printf(" "); + timeInfo = localtime(&ts); + strftime(ts_string, sizeof(ts_string), "%Y%m%d_%H%M%S", timeInfo); + printf("%s ", ts_string); break; } else { - printf("%c", line[k]); + switch(line[k]) + { + case '0': + ts = 0*l + ts; + break; + case '1': + ts = 1*l + ts; + break; + case '2': + ts = 2*l + ts; + break; + case '3': + ts = 3*l + ts; + break; + case '4': + ts = 4*l + ts; + break; + case '5': + ts = 5*l + ts; + break; + case '6': + ts = 6*l + ts; + break; + case '7': + ts = 7*l + ts; + break; + case '8': + ts = 8*l + ts; + break; + case '9': + ts = 9*l + ts; + break; + } + l = l/10; } k++; } @@ -152,23 +191,9 @@ void ak_log_rotate() void ak_log_message(char* program, char* type, char* message) { - struct tm *timeInfo; - time_t ts; - char ts_string[200]; // %Y%Y%Y%Y%m%m%d%d_%H%H%M%M%S%S + time_t ts = time(NULL); + time(&ts); char* some_string = {0}; - ts = time(NULL); - char* timeStampFormat = "%Y%m%d_%H%M%S"; - timeInfo = localtime(&ts); - if ( timeInfo == NULL ) - { - perror("localtime"); - exit(EXIT_FAILURE); - } - if ( strftime(ts_string, sizeof(ts_string), timeStampFormat, timeInfo) == 0 ) - { - fprintf(stderr, "strftime returned 0"); - exit(EXIT_FAILURE); - } if ( program != NULL ) { if ( type != NULL ) @@ -180,18 +205,18 @@ void ak_log_message(char* program, char* type, char* message) printf( "%ld <%s> [%s] %s\n", ts, program, type, message); // out to file though if ( AK_DEBUG ) { - asprintf(&some_string, "%s <%s> [%s] %s", ts_string, program, type, message); + asprintf(&some_string, "%ld <%s> [%s] %s", ts, program, type, message); ak_log_print_log_line(some_string); - // fprintf(stderr, "%s <%s> [%s] %s\n", ts_string, program, type, message); + // fprintf(stderr, "%ld <%s> [%s] %s\n", ts, program, type, message); } } else { // echo "$TS" "<$1>" "[ERROR]" "No message" >> $AK_LOGSFILE - printf("%s <%s> [ERROR] No message\n", ts_string, program); // out to file + printf("%ld <%s> [ERROR] No message\n", ts, program); // out to file if ( AK_DEBUG ) { - fprintf(stderr, "%s <%s> [ERROR] No message\n", ts_string, program); + fprintf(stderr, "%ld <%s> [ERROR] No message\n", ts, program); } exit(1); } @@ -199,10 +224,11 @@ void ak_log_message(char* program, char* type, char* message) else { // echo "$TS" "<$1>" "[ERROR]" "No type and message" >> $AK_LOGSFILE - printf("%s <%s> [ERROR] No type and message\n", ts_string, program); // out to file + + printf("%ld <%s> [ERROR] No type and message\n", ts, program); // out to file if ( AK_DEBUG ) { - fprintf(stderr, "%s <%s> [ERROR] No type and message\n", ts_string, program); + fprintf(stderr, "%ld <%s> [ERROR] No type and message\n", ts, program); } exit(1); } @@ -210,10 +236,10 @@ void ak_log_message(char* program, char* type, char* message) else { // echo "$TS" "<$(basename $0)>" "[ERROR]" "No arguments given" >> $AK_LOGSFILE - printf("%s <%s> [ERROR] No arguments given\n", ts_string, program); // out to file + printf("%ld <%s> [ERROR] No arguments given\n", ts, program); // out to file if ( AK_DEBUG ) { - fprintf(stderr, "%s <%s> [ERROR] No arguments given\n", ts_string, program); + fprintf(stderr, "%ld <%s> [ERROR] No arguments given\n", ts, program); } exit(1); } |