The function prototype of syslog is:
void syslog(int priority, const char *format, ...);
In particular, the second parameter is similar to a printf-style format specifier except that is also supports the specifier %m which will be replaced with an error message generated by strerror(errno).
You could log a simple string along with a priority string by calling this function:
void my_simple_syslog(int priority, const char *message) { static const char * const prio_strings[] = { [LOG_EMERG] = "EMERG", [LOG_ALERT] = "ALERT", [LOG_CRIT] = "CRIT", [LOG_ERR] = "ERR", [LOG_WARNING] = "WARNING", [LOG_NOTICE] = "NOTICE", [LOG_INFO] = "INFO", [LOG_DEBUG] = "DEBUG", }; if (priority < 0 || priority >= sizeof(prio_strings) / sizeof(prio_strings[0]) || !prio_strings[priority]) { /* priority is an unknown value */ syslog(priority, "%s [PRIORITY:%d]", message, priority); } else { syslog(priority, "%s [%s]", message, prio_strings[priority]); } }
This example call:
my_simple_syslog(LOG_ERR, "this is log massage");
produces a log message similar to:
Mar 23 17:56:37 mypc slog[3597]: this is log massage [ERR]
The downside with using this canned approach is that you cannot add extra parameters like you could with calling syslog directly.
prog, then it will be./prog > log.txtprog, then it will be./prog > log.txtSupposeprogis a long-running, critical production process that can't be stopped or restarted, andlog.txtfills up the file system? Logging by redirectingstdout/stderris a BAD IDEA as it ties a process to a specific file - that file can't be deleted, nor can it reliably be truncated to release space.