This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tsg-hasp-tools/platform/include/hasp_log.h
2023-06-26 18:05:38 +08:00

71 lines
2.5 KiB
C

#ifndef _HASP_LOG_H
#define _HASP_LOG_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <time.h>
#include <stdio.h>
#if ENABLD_LOG_FIEL
#define LOG_FILE(prefix, format, ...) \
{ \
FILE *fp = fopen("licenses.log", "a+"); \
if (fp == NULL) \
{ \
break; \
} \
fprintf(fp, "%s " format "\n", prefix, ##__VA_ARGS__); \
fflush(fp); \
fclose(fp); \
}
#else
#define LOG_FILE(prefix, format, ...)
#endif
#define LOG_STOUT(prefix, format, ...) \
{ \
fprintf(stderr, "%s " format "\n", prefix, ##__VA_ARGS__); \
}
#define LOG_INFO(format, ...) \
do \
{ \
char buffer[128] = {0}; \
int n = local_time_string(buffer, sizeof(buffer)); \
snprintf(buffer + n, sizeof(buffer) - n, " tid: %ld", pthread_self()); \
LOG_STOUT(buffer, format, ##__VA_ARGS__); \
LOG_FILE(buffer, format, ##__VA_ARGS__); \
} while (0)
static int local_time_string(char *buff, int size)
{
static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
time_t now;
struct tm local_time;
time(&now);
if (NULL == (localtime_r(&now, &local_time)))
{
return 0;
}
return snprintf(buff, size,
"%s %s %d %02d:%02d:%02d %d",
weekday_str[local_time.tm_wday],
month_str[local_time.tm_mon],
local_time.tm_mday,
local_time.tm_hour,
local_time.tm_min,
local_time.tm_sec,
local_time.tm_year + 1900);
}
#ifdef __cpluscplus
}
#endif
#endif