代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#include "log.h"
#define DEBUG 1
void getsysdate(char *date)
{
// yyyy-mm-dd
time_t now;
struct tm *ntime;
if (-1 == time(&now)) {
snprintf(date, LEN_DATE - 1, "date-unknown");
return;
}
ntime = localtime(&now);
snprintf(date, LEN_DATE - 1, "%d-%d-%d", ntime->tm_year + 1900, ntime->tm_mon + 1,
ntime->tm_mday);
}
void getsystime(char *systime)
{
// hh:mm:ss
time_t now;
struct tm *ntime;
if (-1 == time(&now)) {
snprintf(systime, LEN_TIME - 1, "time-unknown");
return;
}
ntime = localtime(&now);
snprintf(systime, LEN_TIME - 1, "%d:%d:%d", ntime->tm_hour, ntime->tm_min, ntime->tm_sec);
}
int mkdirs(const char *fpath)
{
printf("function:%s\n", __func__);
}
int log2file(const char *fpath, const char *format, va_list args)
{
FILE *fp = NULL;
char str_date[LEN_DATE] = { '\0' };
char absol_fpath[128] = { '\0' };
getsysdate(str_date);
if (access(fpath, F_OK) != 0) {
// make all dirs-------------------tbc
if (-1 == mkdirs(fpath)) {
Log(LOG_DEBUG, "[DEBUG] function:%s --- %s\n", __func__, strerror(errno));
return -1;
}
}
snprintf(absol_fpath, LEN_PATH - 1, "%s%s.log", fpath, str_date);
Log(LOG_DEBUG, "[DEBUG] function:%s --- absol_fpath=%s\n", __func__, absol_fpath);
if (NULL == (fp = fopen(absol_fpath, "a+"))) {
Log(LOG_DEBUG, "[DEBUG] function:%s --- %s", __func__, strerror(errno));
return -1;
}
vfprintf(fp, format, args);
fflush(fp);
fclose(fp);
return 0;
}
int Log(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
switch (level) {
case LOG_INFO:
case LOG_WARNING:
case LOG_ERROR: {
log2file(FLOG_PATH, format, args);
break;
}
case LOG_DEBUG: {
fprintf(stdout, "\033[0;32m"); // color green for debug
vfprintf(stdout, format, args);
fprintf(stdout, "\033[0m");
break;
}
default: {
printf("[DEBUG] function:%s --- log level=%d", __func__, level);
break;
}
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。