简单的跨平台c/c++日志记录
CLog.h
#include <stdlib.h>
#pragma once
#ifndef _CLOG
#define _CLOG
#define CLOG_DEBUG 0
#define CLOG_INFO 1
#define CLOG_WARNING 2
#define CLOG_ERROR 3
#define stderr_message(exp, message) \
{ \
fprintf(stderr,"-------------ERROR-----------------\n"); \
fprintf(stderr,"Failed:%s\nMessage:%s\nLine:%d\nFile:%s\n",#exp,message,__LINE__,__FILE__);\
fprintf(stderr,"-------------ERROR-----------------\n"); \
/*exit(EXIT_FAILURE);*/\
}
//Clog(int level, char *msg)
#define CLog(level,msg)\
{\
_CLog(level,msg,__LINE__,__FILE__);\
}\
//CLogDebug(char *msg)
#define CLogDebug(msg)\
{\
CLog(CLOG_DEBUG,msg);\
}\
//CLogError(char *msg)
#define CLogError(msg)\
{\
CLog(CLOG_ERROR,msg);\
}\
//CLogInfo(char *msg)
#define CLogInfo(msg)\
{\
CLog(CLOG_INFO,msg);\
}\
//CLogWarning(char *msg)
#define CLogWarning(msg)\
{\
CLog(CLOG_WARNING,msg);\
}\
#endif
#ifdef __cplusplus
extern "C" {
#endif
//init clog ! log_path is logfile path!
void InitCLog(char *log_path);
//this fun is logging!
void _CLog(int level, char *msg,int line,char *file);
//freee clog!
void FreeeClog();
#ifdef __cplusplus
}
#endif
CLog.c
#include "CLog.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif
#ifndef _CLOGSTATIC
#define _CLOGSTATIC
const char CLOGLevelName[4][8] = { "debug", "info", "warning", "error" };
FILE *fp = NULL;
char now_file_name[11];
char *log_path;
#ifdef WIN32
CRITICAL_SECTION g_cs;
#else
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
#endif
#endif
void now_date_str(char *date_str)
{
time_t nowtime;
struct tm *timeinfo;
time(&nowtime);
timeinfo = localtime(&nowtime);
sprintf(date_str, "%d_%d_%d",
timeinfo->tm_year + 1900,
timeinfo->tm_mon + 1,
timeinfo->tm_mday);
}
void now_datetime_str(char *datetime_str)
{
time_t nowtime;
struct tm *timeinfo;
time(&nowtime);
timeinfo = localtime(&nowtime);
sprintf(datetime_str, "%d_%d_%d %d:%d:%d",
timeinfo->tm_year + 1900,
timeinfo->tm_mon + 1,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec
);
}
void lock()
{
#ifdef WIN32
EnterCriticalSection(&g_cs);
#else
pthread_mutex_lock(&mutex1);
#endif
}
void unlock()
{
#ifdef WIN32
LeaveCriticalSection(&g_cs);
#else
pthread_mutex_unlock(&mutex1);
#endif
}
long thread_id()
{
#ifdef WIN32
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
void open_log_file()
{
char *filename = (char*)malloc(strlen(log_path) + strlen(now_file_name) + 1);
sprintf(filename, "%s/%s.%s", log_path, now_file_name, "txt");
fp = fopen(filename, "ab+");
if (fp == NULL)
{
stderr_message(fp == NULL, "(clog) fopen error !");
return;
}
}
//mk dirs
void mkdir_p(char *dir)
{
//copy
char * dir_c = (char*)malloc(strlen(dir) + 1);
memcpy(dir_c, dir, strlen(dir) + 1);
dir = dir_c;
char * temppath = (char*)malloc(strlen(dir) + 1);
int tempindex = 0;
while (*dir_c != '\0')
{
if (*dir_c == '\\')
*dir_c = '/';
if (*dir_c == '/')
{
tempindex = dir_c - dir;
memcpy(temppath, dir, tempindex);
temppath[tempindex] = '\0';
if (_access(temppath, 0) != 0)
_mkdir(temppath);
}
dir_c++;
}
if (_access(dir, 0) != 0)
_mkdir(dir);
free(dir);
free(temppath);
}
void InitCLog(char *path)
{
#ifdef WIN32
InitializeCriticalSection(&g_cs);
#endif
if (path == NULL)
stderr_message(path == NULL, "(clog) logpath is null !");
now_date_str(now_file_name);
int pathlength = strlen(path);
log_path = (char*)malloc(pathlength*sizeof(char) + 1);
strcpy(log_path, path);
if (log_path[pathlength - 1] == '/')
log_path[pathlength - 1] = '\0';
mkdir_p(log_path);
open_log_file();
}
void _CLog(int level, char *msg, int line, char *file)
{
lock();
if (level<0 || level>3)
{
stderr_message(level<0 || level>3, "(clog) level overflow!");
return;
}
if (fp == NULL)
{
stderr_message(fp == NULL, "(clog) clog not init!");
return;
}
char temp_now_file_name[11];
now_date_str(temp_now_file_name);
if (strcmp(temp_now_file_name, now_file_name) != 0)
{
strcpy(now_file_name, temp_now_file_name);
fclose(fp);
open_log_file();
if (fp == NULL)
{
stderr_message(fp == NULL, "(clog) clog init error!");
return;
}
}
char* info = (char*)malloc(strlen(msg) + 80 + strlen(file));
char datetimestr[21];
now_datetime_str(datetimestr);
sprintf(info, "%s thread:%d %s \r\n%s\r\nfile:%s line:%d\r\n",
datetimestr,
thread_id(),
CLOGLevelName[level],
msg,
file,
line);
fwrite(info, strlen(info), 1, fp);
fflush(fp);
free(info);
unlock();
}
void FreeeClog()
{
lock();
if (fp != NULL)
fclose(fp);
free(log_path);
unlock();
}
Example:
#include "stdafx.h"//Without this header file under linux
#include "CLog.h"
#include <stdlib.h>
#ifdef WIN32
#include <Windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#define thread_num 10
void thread_fun()
{
int i = 0;
for (; i < 1000; i++)
{
CLog(CLOG_DEBUG, "this debug message!");
CLog(CLOG_INFO, "this info!╮(╯▽╰)╭");
CLog(CLOG_WARNING, "警告!this warning!!");
CLog(CLOG_ERROR, "Error!!");
}
}
#ifdef WIN32
unsigned _stdcall ThreadProc(void* param)
{
thread_fun();
return 0;
}
void win32_thread_test()
{
HANDLE handle[thread_num];
int i = 0;
for (; i < thread_num; i++)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, NULL, NULL, NULL);
}
for (i = 0; i < thread_num; i++)
{
WaitForSingleObject(handle[i], INFINITE);
CloseHandle(handle[i]);
}
}
#else
void *linux_threadfun(void *arg)
{
thread_fun();
}
void linux_thread_test()
{
pthread_t pthreadarray[thread_num];
int i = 0;
for (; i < thread_num; i++)
{
pthreadarray[i] = pthread_create(&pthreadarray[i], NULL, linux_threadfun, NULL);
}
for (i = 0; i < thread_num; i++)
{
pthread_join(&pthreadarray[i], NULL);
}
}
#endif
int main()
{
/*Example:
InitCLog ("D:/ntg/logs"); disk directory
InitCLog ("logs"); the current directory under the logs directory
InitCLog ("test/log/codelog"); the current directory est/log/codelog directory
*/
InitCLog("D:/Logs");
//Loging Example:
CLog(CLOG_DEBUG, "this debug message!");
CLog(CLOG_INFO, "this info!╮(╯▽╰)╭");
CLog(CLOG_WARNING, "this warning! ( ⊙ o ⊙ )啊!");
CLog(CLOG_ERROR, "Error!!");
//or
CLogInfo("这是info!");
CLogError("报错!");
CLogDebug("debug!!");
CLogWarning("指针为null!");
//Multithreaded Test Start
#ifdef WIN32
win32_thread_test();
#else
linux_thread_test();
#endif
//Multithreaded Test End
//The program exits when the need to call FreeeClog free
FreeeClog();
system("pause");
return 0;
}
简单的跨平台c/c++日志记录的更多相关文章
- Ibatis.net 1.6.2 简单配置及Log4.Net日志记录
看似简单的Ibatis配置起来注意的地方还挺多.里面的设置及功能还算强大.昨晚配置了半宿,结果一运行还是各种错误.急的直冒汗.果断下载实例去研究.英文不好文档只能作为功能参考. 下面一步步进入Ibat ...
- 【转】使用Log4Net进行日志记录
首先说说为什么要进行日志记录.在一个完整的程序系统里面,日志系统是一个非常重要的功能组成部分.它可以记录下系统所产生的所有行为,并按照某种规范表达出来.我们可以使用日志系统所记录的信息为系统进行排错, ...
- php 简单通用的日志记录方法
使用file_put_contents 方法来实现简单便捷的日志记录功能 方法1: // use \r\n for new line on windows, just \n on linux func ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...
- Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验
Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验 前几天分享的"[Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验][http://www ...
- Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验
Net Core平台灵活简单的日志记录框架NLog初体验 前几天分享的"[Net Core集成Exceptionless分布式日志功能以及全局异常过滤][https://www.cnblog ...
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
本文梯子 本文3.0版本文章 代码已上传Github+Gitee,文末有地址 大神反馈: 零.今天完成的深红色部分 一.AOP 之 实现日志记录(服务层) 1.定义服务接口与实现类 2.在API层中添 ...
- 简单的C#日志记录和弹出提示
记录这个博客的想法只有两点, 一,在使用webserver的时候如果你不好调用本地server端,那么你可以启用日志来查看异常.当然也可以调用本地的server端.这里简单说一下.不是本文的重点. 发 ...
- C++实现简单的日志记录
C++实现简单的日志记录 //dlogger.h #ifndef DLOGGER_H #define DLOGGER_H #include <iostream> #include < ...
随机推荐
- 如何解决SoftekBarcode.dll加载失败的问题
本文转自:慧都控件网 Softek Barcode Reader Toolkit是专门从事于条形码读取技术的软件公司Softek旗下一款著名的条码读取工具包.最近有部分用户反映在运行此工具包时会遇到“ ...
- 02、AngularJs的数据绑定
我们知道,AngularJs中的数据绑定是双向绑定的,View的改变,会改变Model,Model的改变也会改变View中的值,废话不多说,我们直接上代码. <!DOCTYPE html> ...
- ios图片添加文字或者水印
在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...
- 练习1-21:编写程序entab,将空格串替换为最少数量的制表符和空格。。。(C程序设计语言 第2版)
#include <stdio.h> #define N 5 main() { int i, j, c, lastc; lastc = 'a'; i = j = ; while ((c=g ...
- Inno Setup 如何读写文件
软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...
- 初入liunx的一些基本的知识
本系列的博客来自于:http://www.92csz.com/study/linux/ 在此,感谢原作者提供的入门知识 这个系列的博客的目的在于将比较常用的liunx命令从作者的文章中摘录下来,供自己 ...
- android知识杂记(一)
记录项目中用的零碎知识点,用以备忘. android:screenOrientation:portrait 限制横屏 activity启动状态 singleTop 只执行一次,通常用在欢迎页面 sin ...
- 在UWP应用中加入Cortana语音指令集
本文介绍小娜语音指令集的使用场景,如何将UWP应用接入小娜的语音指令集,使用户直接通过小娜启动应用并使用应用中 一些轻量级的功能.文中以必应词典作为实例讲解必应词典UWP版本是如何接入小娜语音功能的. ...
- 假期实践作业:从IT角度看地铁
实习时间:2016/02/23——2016/02/26 实习地点:京港地铁14号线 实习报告: 大学四年过得真快,转眼就大三了,大学前两年半的生活可谓多姿多彩,从不懂计算机到对编程感兴趣,期待得最多的 ...
- 团队项目——站立会议DAY6
团队项目--站立会议 DAY6 团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱 今日(2016/5/13),站立会议已进行了一周时间,大家将这一周所遇到的问题和 ...