本文整理汇总了C++中LogFile::set_log_fd方法的典型用法代码示例。如果您正苦于以下问题:C++ LogFile::set_log_fd方法的具体用法?C++ LogFile::set_log_fd怎么用?C++ LogFile::set_log_fd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LogFile
的用法示例。
在下文中一共展示了LogFile::set_log_fd方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
static inline void get_log_target()
{
logTarget = AM_LOG_TARGET_NULL;
const char *target = empty_target_str(getenv(AM_TARGET_ENV_VAR));
if (is_str_equal(target, "stderr")) {
logTarget = AM_LOG_TARGET_STDERR;
} else if (is_str_equal(target, "syslog")) {
logTarget = AM_LOG_TARGET_SYSLOG;
} else if (is_str_start_with(target, "file:")) {
int fd = -1;
char logname[512] = {0};
time_t current = time(NULL);
struct tm* tmstruct = gmtime(¤t);
const char *filename = strstr(target, ":");
const char* ext = strrchr(&filename[1], '.');
if (ext) {
snprintf(logname, (ext - &filename[1]) + 1, "%s", &filename[1]);
sprintf(logname + strlen(logname), "-%04d%02d%02d%02d%02d%02d",
tmstruct->tm_year + 1900, tmstruct->tm_mon + 1,
tmstruct->tm_mday, tmstruct->tm_hour,
tmstruct->tm_min, tmstruct->tm_sec);
sprintf(logname + strlen(logname), "%s", ext);
} else {
sprintf(logname, "%s-%04d%02d%02d%02d%02d%02d.log",
&filename[1],
tmstruct->tm_year + 1900,
tmstruct->tm_mon + 1,
tmstruct->tm_mday,
tmstruct->tm_hour,
tmstruct->tm_min,
tmstruct->tm_sec);
}
if ((fd = open(logname,
O_WRONLY|O_TRUNC|O_CREAT,
S_IRUSR|S_IWUSR)) >= 0) {
logfile.set_log_fd(fd);
logTarget = AM_LOG_TARGET_FILE;
} else {
fprintf(stderr,
"Failed to open file %s: %s(Reset log target to stderr)",
&filename[1], strerror(errno));
logTarget = AM_LOG_TARGET_STDERR;
}
} else if (is_str_equal(target, "null")) {
logTarget = AM_LOG_TARGET_NULL;
}
}
示例2: vsnprintf
static inline void am_level_logv(const char *module,
AmLogLevel level,
const char *format,
va_list vlist)
{
char text[2*1024] = {0};
char str[4*1024] = {0};
int len = vsnprintf(text, sizeof(text), format, vlist);
if (AM_LIKELY((uint32_t)len < sizeof(text))) {
text[len] = '\0';
} else {
text[sizeof(text) - 1] = '\0';
}
logTimeStamp ?
snprintf(str, sizeof(str) - 1, "[%s]%s\n", get_timestamp(), text) :
snprintf(str, sizeof(str) - 1, "%s\n", text);
AM_LOCK();
switch(logTarget) {
case AM_LOG_TARGET_STDERR: {
fprintf(stderr, "%s", str);
}break;
case AM_LOG_TARGET_SYSLOG: {
openlog(empty_module_name(module), LOG_PID, LOG_USER);
syslog(level_to_syslog[level], "%s\n", text);
}break;
case AM_LOG_TARGET_FILE: {
if ((write(logfile.fd(), str, strlen(str)) < 0)) {
fprintf(stderr, "%s: %s: %d\n",
B_RED("Failed writing logs to file. Redirect log to console"),
strerror(errno), logfile.fd());
logfile.set_log_fd(-1);
logTarget = AM_LOG_TARGET_STDERR;
fprintf(stderr, "%s", str);
}
}break;
case AM_LOG_TARGET_NULL:
default: break;
}
AM_UNLOCK();
}
示例3: close_log_file
void close_log_file()
{
logfile.set_log_fd(-1);
}