int main(int argc, char *argv[]) { char logname[12]; if (argc != 4) return -1; sprintf(logname, "tester%d", atoi(argv[1])); mylog_open(logname); sleep(atoi(argv[2])); mylog_write("log test 1"); sleep(atoi(argv[3])); mylog_write("Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! Too long! "); mylog_close(); return 0; }
void mylog_close_and_free() { mylog_close(); if (_log != NULL) { #ifdef HAVE_CLOSELOG if (_log->useSyslog == 1) closelog(); #endif //HAVE_CLOSELOG if (_log->file != NULL) free(_log->file); free(_log); _log = NULL; } }
void mylog_printf(int level, const char *str, ...) { va_list ap; char buffer[1024]; size_t size; if (_log != NULL && _log->useSyslog == 1) { int logprio; switch (level) { case MYLOG_DEBUG: logprio = LOG_DEBUG; break; case MYLOG_WARNING: logprio = LOG_WARNING; break; case MYLOG_ERROR: logprio = LOG_ERR; break; case MYLOG_TRANSFERT: logprio = LOG_NOTICE; break; default: logprio = LOG_INFO; break; } va_start(ap, str); vsyslog(logprio, str, ap); va_end(ap); } if (_log != NULL && _log->file != NULL) { struct tm *tm; time_t t; char fmt[1024]; if (_log->nextReopen == 1) { _log->nextReopen = 0; mylog_close(); mylog_open(_log->file, _log->useSyslog); } t = time(NULL); if ((tm = localtime(&t)) == NULL) { if (snprintf(fmt, sizeof(buffer), "[Error with time] [%i]%s\n", _log->pid, str) > 0) goto forceShowLog; return; } #ifndef HAVE_LOG_IN_COLOR if (snprintf(fmt, sizeof(buffer), "%i-%02i-%02i %02i:%02i:%02i [%i]%s\n", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, _log->pid, str) > 0) #else if (level < 0 || level >= MYLOG_MAX) level = MYLOG_ERROR; if (snprintf(fmt, sizeof(buffer), "%i-%02i-%02i %02i:%02i:%02i \33[%i:%i:%im[%i]%s\33[37:40:0m\n", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, _log->color[level][0], _log->color[level][1], _log->color[level][2], _log->pid, str) > 0) #endif { forceShowLog: va_start(ap, str); if ((size = vsnprintf(buffer, sizeof(buffer), fmt, ap)) > 0) (void) write(_log->fd, buffer, size); va_end(ap); } } }