static void log_rotate (Gstreamill *gstreamill) { gchar *log_path; Job *job; GSList *list; /* gstreamill log rotate. */ log_path = g_build_filename (gstreamill->log_dir, "gstreamill.log", NULL); rotate_log (gstreamill, log_path, getpid ()); g_free (log_path); /* jobs log rotate. */ list = gstreamill->job_list; while (list != NULL) { job = list->data; if ((job->worker_pid == 0) || !(job->is_live)) { /* pid == 0 and non-live job, do not care about job which is stoped */ list = list->next; continue; } log_path = g_build_filename (gstreamill->log_dir, job->name, "gstreamill.log", NULL); rotate_log (gstreamill, log_path, job->worker_pid); g_free (log_path); list = list->next; } }
int main (int argc, char *argv[]) { int max_files = 0; const char *log_file = argv[1]; unsigned flags = 0; if (argc > 2) max_files = atoi(argv[2]); if (max_files < 1) max_files = 10; if (argc > 3) flags = atoi(argv[3]); if (argc > 4) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = SA_NOCLDWAIT; sigaction(SIGCHLD, &sa, NULL); if (fork() == 0) { sleep(30); fprintf(stderr, "child(%d) exiting...\n", getpid()); exit(0); } } rotate_log(log_file, max_files, flags); if (flags & RLF_COMPRESS) system("ps axl"); exit(0); }
void log_status_skip(LogInstance instance, const gchar *format, ...) { va_list args; gchar buf[BUFFSIZE + LOG_TIME_LEN]; time_t t; LogText *logtext = g_new0(LogText, 1); struct tm buft; time(&t); strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft)); va_start(args, format); g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args); va_end(args); if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN); logtext->instance = instance; logtext->text = g_strdup(buf + LOG_TIME_LEN); logtext->type = LOG_STATUS_SKIP; g_timeout_add(0, invoke_hook_cb, logtext); if (log_fp[instance] && prefs_common_enable_log_status()) { FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance]) FPUTS("* SKIPPED: ", log_fp[instance]) log_size[instance] += strlen("* SKIPPED: "); FPUTS(buf + LOG_TIME_LEN, log_fp[instance]) log_size[instance] += strlen(buf); FFLUSH(log_fp[instance]) rotate_log(instance); } }
static int hlog_write(int priority, const char *s) { struct tm lt; struct timeval tv; char wb[LOG_LEN]; int len, w; gettimeofday(&tv, NULL); gmtime_r(&tv.tv_sec, <); if (log_dest & L_STDERR) { rwl_rdlock(&log_file_lock); fprintf(stderr, "%4d/%02d/%02d %02d:%02d:%02d.%06d %s[%d:%lx] %s: %s\n", lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, (int)tv.tv_usec, (log_name) ? log_name : "aprsc", (int)getpid(), (unsigned long int)pthread_self(), log_levelnames[priority], s); rwl_rdunlock(&log_file_lock); } if ((log_dest & L_FILE) && (log_file >= 0)) { len = snprintf(wb, LOG_LEN, "%4d/%02d/%02d %02d:%02d:%02d.%06d %s[%d:%lx] %s: %s\n", lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, (int)tv.tv_usec, (log_name) ? log_name : "aprsc", (int)getpid(), (unsigned long int)pthread_self(), log_levelnames[priority], s); if (len >= LOG_LEN) { len = LOG_LEN-1; // Truncated! Do not write out the NUL byte in the end. wb[LOG_LEN-2] = '\n'; // Do write out a newline which was truncated. } wb[LOG_LEN-1] = 0; rwl_rdlock(&log_file_lock); if ((w = write(log_file, wb, len)) != len) fprintf(stderr, "aprsc logger: Could not write to %s (fd %d): %s\n", log_fname, log_file, strerror(errno)); rwl_rdunlock(&log_file_lock); if (log_rotate_size) { off_t l = lseek(log_file, 0, SEEK_CUR); if (l >= log_rotate_size) { rotate_log(); } } } if (log_dest & L_SYSLOG) { rwl_rdlock(&log_file_lock); syslog(priority, "%s: %s", log_levelnames[priority], s); rwl_rdunlock(&log_file_lock); } return 1; }
bool CSqlLogger::write_log(const std::string& sql) { try { int32_t length = static_cast<int32_t>(sql.size()); sys::LockHelper<sys::CLock> lock_helper(_lock); if ((-1 == _log_fd) || need_rotate()) { rotate_log(); } if (-1 == _log_fd) { return false; } struct iovec iov[2]; iov[0].iov_base = &length; iov[0].iov_len = sizeof(length); iov[1].iov_base = const_cast<char*>(sql.data()); iov[1].iov_len = sql.size(); ssize_t bytes_written = writev(_log_fd, iov, sizeof(iov)/sizeof(iov[0])); if (bytes_written != static_cast<int>(iov[0].iov_len+iov[1].iov_len)) { const int errcode = errno; THROW_SYSCALL_EXCEPTION(utils::CStringUtils::format_string("writev %s error: %s", _log_filepath.c_str(), sys::Error::to_string(errcode).c_str()), errcode, "writev"); } // 计数 ++_total_lines; const int32_t lines = argument::lines->value(); if ((lines > 0) && (++_num_lines >= lines)) { _num_lines = 0; if (-1 == fdatasync(_log_fd)) { const int errcode = errno; THROW_SYSCALL_EXCEPTION(utils::CStringUtils::format_string("fdatasync %s error: %s", _log_filepath.c_str(), sys::Error::to_string(errcode).c_str()), errno, "fdatasync"); } } return true; } catch (sys::CSyscallException& ex) { MYLOG_ERROR("[%s] write [%s] to [%s] failed: %s\n", _dbinfo->str().c_str(), sql.c_str(), _log_filepath.c_str(), ex.str().c_str()); return false; } }
void logger_rotational(const char * module, const char * msg) { time_t rawtime; struct tm * timeinfo; time(&rawtime); if(rawtime >= log_rotate_time) { rotate_log(); } timeinfo = localtime(&rawtime); char buffer[MAX_LOG_LINE]; int len = ::snprintf(buffer, MAX_LOG_LINE-1, "%02d:%02d:%02d:%08lX:<%s>: ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, (unsigned long)rawtime, module); util::strncpy(buffer + len, msg, MAX_LOG_LINE - len); //buffer[MAX_LOG_LINE-1] = '\0'; //log_line(buffer); logger_file(buffer); }