Пример #1
0
/*
 * log function
 */
void _applog(int prio, const char *str, bool force)
{
#ifdef HAVE_SYSLOG_H
	if (use_syslog) {
		syslog(prio, "%s", str);
	}
#else
	if (0) {}
#endif
	else {
		char datetime[64];
		struct timeval tv = {0, 0};
		struct tm *tm;

		cgtime(&tv);

		const time_t tmp_time = tv.tv_sec;
		tm = localtime(&tmp_time);

		/* Day changed. */
		if (opt_log_show_date && (last_date_output_day != tm->tm_mday))
		{
			last_date_output_day = tm->tm_mday;
			char date_output_str[64];
			snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
				tm->tm_year + 1900,
				tm->tm_mon + 1,
				tm->tm_mday);
			_applog(prio, date_output_str, force);
			
		}

		if (opt_log_show_date)
		{
			snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
				tm->tm_year + 1900,
				tm->tm_mon + 1,
				tm->tm_mday,
				tm->tm_hour,
				tm->tm_min,
				tm->tm_sec);
		}
		else
		{
			snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
				tm->tm_hour,
				tm->tm_min,
				tm->tm_sec);
		}

		/* Only output to stderr if it's not going to the screen as well */
		if (!isatty(fileno((FILE *)stderr))) {
			fprintf(stderr, "%s%s\n", datetime, str);	/* atomic write to stderr */
			fflush(stderr);
		}

		my_log_curses(prio, datetime, str, force);
	}
}
Пример #2
0
/*
 * log function
 */
void _applog(int prio, const char *str, bool force)
{
#ifdef HAVE_SYSLOG_H
	if (use_syslog) {
		syslog(prio, "%s", str);
	}
#else
	if (0) {}
#endif
	else {
		char datetime[64];
		struct timeval tv = {0, 0};
		struct tm *tm;

		cgtime(&tv);

		const time_t tmp_time = tv.tv_sec;
		tm = localtime(&tmp_time);

		snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d] ",
			tm->tm_year + 1900,
			tm->tm_mon + 1,
			tm->tm_mday,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec);

		/* Only output to stderr if it's not going to the screen as well */
		if (!isatty(fileno((FILE *)stderr))) {
			fprintf(stderr, "%s%s\n", datetime, str);	/* atomic write to stderr */
			fflush(stderr);
		}

		if(g_logfile_enable) {
			if(!g_log_file) {
				g_log_file = fopen(g_logfile_path, g_logfile_openflag);
			}
			if(g_log_file) {
				fwrite(datetime, strlen(datetime), 1, g_log_file);
				fwrite(str, strlen(str), 1, g_log_file);
				fwrite("\n", 1, 1, g_log_file);
				fflush(g_log_file);
			}
		}

		my_log_curses(prio, datetime, str, force);
	}
}
Пример #3
0
void vapplog(int prio, const char *fmt, va_list ap)
{
	if (!opt_debug && prio == LOG_DEBUG)
		return;

#ifdef HAVE_SYSLOG_H
	if (use_syslog) {
		vsyslog(prio, fmt, ap);
	}
#else
	if (0) {}
#endif
	else if (opt_log_output || prio <= LOG_NOTICE) {
		char *f;
		int len;
		struct timeval tv = {0, 0};
		struct tm *tm;

		gettimeofday(&tv, NULL);

		tm = localtime(&tv.tv_sec);

		len = 40 + strlen(fmt) + 22;
		f = alloca(len);
		sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
			tm->tm_year + 1900,
			tm->tm_mon + 1,
			tm->tm_mday,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec,
			fmt);
		/* Only output to stderr if it's not going to the screen as well */
		if (!isatty(fileno((FILE *)stderr))) {
			va_list apc;

			va_copy(apc, ap);
			vfprintf(stderr, f, apc);	/* atomic write to stderr */
			fflush(stderr);
		}

		my_log_curses(prio, f, ap);
	}
}
Пример #4
0
/*
 * generic log function used by priority specific ones
 * equals vapplog() without additional priority checks
 */
static void log_generic(int prio, const char *fmt, va_list ap)
{
#ifdef HAVE_SYSLOG_H
	if (use_syslog) {
		vsyslog(prio, fmt, ap);
	}
#else
	if (0) {}
#endif
	else {
		char *f;
		int len;
		struct timeval tv = {0, 0};
		struct tm *tm;

		gettimeofday(&tv, NULL);

		time_t tt = tv.tv_sec;
		tm = localtime(&tt);

		len = 40 + strlen(fmt) + 22;
		f = (char*)alloca(len+20);
		sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
			tm->tm_year + 1900,
			tm->tm_mon + 1,
			tm->tm_mday,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec,
			fmt);
		/* Only output to stderr if it's not going to the screen as well */
		if (!isatty(fileno((FILE *)stderr))) {
			va_list apc;

			va_copy(apc, ap);
			vfprintf(stderr, f, apc);	/* atomic write to stderr */
			fflush(stderr);
		}

		my_log_curses(prio, f, ap);
	}
}
Пример #5
0
/*
 * log function
 */
void _applog(int prio, const char *str)
{
#ifdef HAVE_SYSLOG_H
	if (use_syslog) {
		syslog(prio, "%s", str);
	}
#else
	if (0) {}
#endif
	else {
		char datetime[64];
		struct timeval tv = {0, 0};
		struct tm *tm;

		cgtime(&tv);

		const time_t tmp_time = tv.tv_sec;
		tm = localtime(&tmp_time);

		sprintf(datetime, " [%d-%02d-%02d %02d:%02d:%02d] ",
			tm->tm_year + 1900,
			tm->tm_mon + 1,
			tm->tm_mday,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec);

		/* Only output to stderr if it's not going to the screen as well */
		if (!isatty(fileno((FILE *)stderr))) {
			fprintf(stderr, "%s%s\n", datetime, str);	/* atomic write to stderr */
			fflush(stderr);
		}

		my_log_curses(prio, datetime, str);
	}
}