00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 #include <string.h>
00034 #include <stdarg.h>
00035 #include <unistd.h>
00036 #include "qDecoder.h"
00037
00038 static int _realOpen(Q_LOG *log);
00039
00061 Q_LOG *qLogOpen(const char *logbase, const char *filenameformat, int rotateinterval, bool flush) {
00062 Q_LOG *log;
00063
00064
00065 if ((log = (Q_LOG *)malloc(sizeof(Q_LOG))) == NULL) return NULL;
00066
00067
00068 qStrCpy(log->logbase, sizeof(log->logbase), logbase, sizeof(log->logbase));
00069 qStrCpy(log->nameformat, sizeof(log->nameformat), filenameformat, sizeof(log->nameformat));
00070 log->fp = NULL;
00071 log->console = false;
00072 log->rotateinterval = ((rotateinterval > 0) ? rotateinterval : 0);
00073 log->nextrotate = 0;
00074 log->flush = flush;
00075
00076 if (_realOpen(log) == 0) {
00077 qLogClose(log);
00078 return NULL;
00079 }
00080
00081 return log;
00082 }
00083
00091 bool qLogClose(Q_LOG *log) {
00092 if (log == NULL) return false;
00093 if (log->fp != NULL) {
00094 fclose(log->fp);
00095 log->fp = NULL;
00096 }
00097 free(log);
00098 return true;
00099 }
00100
00109 bool qLogSetConsole(Q_LOG *log, bool consoleout) {
00110 if (log == NULL) return false;
00111 log->console = consoleout;
00112 return true;
00113 }
00114
00122 bool qLogFlush(Q_LOG *log) {
00123 if (log == NULL || log->fp == NULL) return false;
00124
00125 if (log->flush == true) return true;
00126
00127 if(fflush(log->fp) == 0) return true;
00128 return false;
00129 }
00130
00139 bool qLog(Q_LOG *log, const char *format, ...) {
00140 char buf[1024];
00141 va_list arglist;
00142 time_t nowTime = time(NULL);
00143
00144 if (log == NULL || log->fp == NULL) return false;
00145
00146 va_start(arglist, format);
00147 vsnprintf(buf, sizeof(buf), format, arglist);
00148 va_end(arglist);
00149
00150
00151 if (log->console == true) fprintf(stderr, "%s\n", buf);
00152
00153
00154 if (log->nextrotate > 0 && nowTime >= log->nextrotate) {
00155 _realOpen(log);
00156 }
00157
00158
00159 if (fprintf(log->fp, "%s\n", buf) < 0) return false;
00160
00161
00162 if (log->flush == true) fflush(log->fp);
00163
00164 return true;
00165 }
00166
00168
00170
00171 static int _realOpen(Q_LOG *log) {
00172 time_t nowtime = time(NULL);
00173
00174
00175 strftime(log->filename, sizeof(log->filename), log->nameformat, localtime(&nowtime));
00176 snprintf(log->logpath, sizeof(log->logpath), "%s/%s", log->logbase, log->filename);
00177 if (log->fp != NULL) fclose(log->fp);
00178 if ((log->fp = fopen(log->logpath, "a")) == NULL) return 0;
00179
00180
00181 if (log->rotateinterval > 0) {
00182 time_t ct = time(NULL);
00183 time_t dt = ct - mktime(gmtime(&ct));
00184 log->nextrotate = (((ct + dt) / log->rotateinterval) + 1) * log->rotateinterval - dt;
00185 } else {
00186 log->nextrotate = 0;
00187 }
00188
00189 return 1;
00190 }