linux:
#pragma once #include <chrono> static std::time_t getTimeStamp() { std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); auto tmp=std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()); std::time_t timestamp = tmp.count(); return timestamp; } static std::tm* gettm(uint64_t timestamp) { uint64_t milli = timestamp; milli += (uint64_t)8*60*60*1000;//add to beijing time zone. auto mTime = std::chrono::milliseconds(milli); auto tp=std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds>(mTime); auto tt = std::chrono::system_clock::to_time_t(tp); std::tm* now = std::gmtime(&tt); return now; } static std::string getTimeStr() { time_t timep; timep = getTimeStamp(); struct tm *info; info = gettm(timep); char tmp[27] = {0}; sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d.%06ld", info->tm_year+1900, info->tm_mon+1, info->tm_mday, info->tm_hour, info->tm_min, info->tm_sec, timep%1000000); return tmp; } #define DEBUG_FLAG #ifdef DEBUG_FLAG #define MY_LOGD(format, ...) {printf("%s: D/%s[%s]: ", getTimeStr().c_str(), basename(__FILE__), __func__);printf(format, ##__VA_ARGS__);printf("\n");} #else #define MY_LOGD(format, ...) #endif #define MY_LOGE(format, arg...) {printf("%s: E/%s[%s]: ", getTimeStr().c_str(), basename(__FILE__), __func__);if(strlen(format)>0){printf(format, ##arg);}printf("\n");} #define FUNCTION_NAME MY_LOGD(" "); #define FUNCTION_IN MY_LOGD("+") #define FUNCTION_OUT MY_LOGD("-")
windows:
#pragma once #include <chrono> #ifdef WIN32 #include <ctime> #include <filesystem> #endif static std::time_t getTimeStamp() { std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()); std::time_t timestamp = tmp.count(); return timestamp; } static std::tm* gettm(uint64_t timestamp) { uint64_t milli = timestamp; milli += (uint64_t)8 * 60 * 60 * 1000;//add to beijing time zone. auto mTime = std::chrono::milliseconds(milli); auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime); auto tt = std::chrono::system_clock::to_time_t(tp); std::tm* now = std::gmtime(&tt); return now; } static std::string getTimeStr() { time_t timep; timep = getTimeStamp(); struct tm *info; info = gettm(timep); char tmp[27] = { 0 }; sprintf_s(tmp, "%04d-%02d-%02d %02d:%02d:%02d.%06ld", info->tm_year + 1900, info->tm_mon + 1, info->tm_mday, info->tm_hour, info->tm_min, info->tm_sec, (long)timep % 1000000); return tmp; } static void string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst) { std::string::size_type pos = 0; std::string::size_type srclen = strsrc.size(); std::string::size_type dstlen = strdst.size(); while ((pos = strBig.find(strsrc, pos)) != std::string::npos) { strBig.replace(pos, srclen, strdst); pos += dstlen; } } static std::string GetPathOrURLShortName(std::string strFullName) { if (strFullName.empty()) { return ""; } string_replace(strFullName, "/", "\\"); std::string::size_type iPos = strFullName.find_last_of('\\') + 1; return strFullName.substr(iPos, strFullName.length() - iPos); } #define DEBUG_FLAG #ifdef DEBUG_FLAG #define MY_LOGD(format, ...) {printf("%s: D/%s[%s]: ", getTimeStr().c_str(), GetPathOrURLShortName(__FILE__).c_str(), __func__);printf(format, ##__VA_ARGS__);printf("\n");} #else #define MY_LOGD(format, ...) #endif #define MY_LOGE(format, ...) {printf("%s: E/%s[%s]: ", getTimeStr().c_str(), GetPathOrURLShortName(__FILE__).c_str(), __func__);if(strlen(format)>0){printf(format, ##__VA_ARGS__);}printf("\n");} #define FUNCTION_NAME MY_LOGD(" "); #define FUNCTION_IN MY_LOGD("+") #define FUNCTION_OUT MY_LOGD("-")
save file:
#include <ctime> #include <iostream> #include <fstream> #include <stdio.h> #include <stdarg.h> #include <direct.h> #include <string> #include <io.h> #include <stdlib.h> #include <sys/timeb.h> #include <time.h> #include <fcntl.h> /****************************** * 初始化日志 ******************************/ static FILE *logFile; void initLogFile(const char* dir="e:/") { time_t seconds = time(NULL); //获取时间 struct tm *p; p = localtime(&seconds);//获取本地时间 char filePath[100] = {0}; sprintf_s(filePath,"%s/log%04d-%02d-%02d_%02d%02d%02d.log",dir,1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec); logFile = fopen(filePath, "w"); if ( logFile == NULL ) { printf("can not open log file: %s/n", filePath); } } /****************************** * 打印日志 ******************************/ void log(const char *fmt ...) { if(logFile == NULL) { initLogFile(); } //获取本地时间 time_t seconds = time(NULL); struct tm *p; p = localtime(&seconds); // 毫秒 struct timeb tb; ftime(&tb); char prefix[100] = {0}; sprintf_s(prefix, "[%04d-%02d-%02d %02d:%02d:%02d.%03d] >> ",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec, tb.millitm); fprintf(logFile, "%s", prefix); va_list vp; va_start(vp, fmt); vfprintf(logFile, fmt, vp); //fprintf(logFile, "\n"); va_end(vp); fflush(logFile); } /****************************** * 打印日志(无前缀) ******************************/ void logNoPrefix(const char *fmt ...) { if(logFile == NULL) { initLogFile(); } va_list vp; va_start(vp, fmt); vfprintf(logFile, fmt, vp); va_end(vp); fflush(logFile); }
相关推荐
自己写的c++实现的单例模式日志类。支持多线程、日志等级、可输出每条日志的输出时间(精确到毫秒级),日志级别,支持可变长参数、标准格式化输出。 代码中包含VS2010示例程序,使用方便。
具体代码如下:* @FileName vsnprintf_name_line.c* @Describe A simple example for using v
/* 跨平台日志函数,Linux下与windows下亲测有效 */ /*****************************************************/ #ifndef _LOG_FORMAT_H_ #define _LOG_FORMAT_H_ // 日志等级 enum LogLevel { _LOG_TRACE, _LOG_INFO,...
将格式化字符串输出到log文件中,代码会在执行文件的目录,自动创建子目录 log\ 并在其中创建跟执行文件同名的log文件 当记录的log文件超过5M时,会将log压缩重命名,并创建新的log文件 用户使用时,只可调用以 ...
C语言常用的调试用函数,例如打印参数名和数值、记录日志文件、格式化打印一维和二维数组、二进制信息等。头文件包含:1、常用数据类型别名;2、重定向printf到日志文件,打印信息可选包含行号,便于调试信息追溯;3...
分等级,带格式的日志文件打印。 2.1.3. 配置器(Configurator) 从指定的配置文件中加载配置信息。 2.1.4. 多路输入输出(MultiIo) 封装epoll多路I/O系统调用,提供增加、删除和等待操作接口。 2.1.5. 插件管理器...
1、RAW格式:未经过格式化的原始日志。 2、标签:在软件中可以按照文件、模块、功能等方面,对需要打印的日志设定标签,实现日志分类。 1.2 插件 1、Flash:使用 EasyFlash 库提供的Flash操作接口,无需文件系统,...
实例169 格式化磁盘 6.3 桌面相关设置 实例170 隐藏、显示“开始”按钮 实例171 隐藏、显示桌面文件 实例172 隐藏、显示Windows任务栏 6.4 系统相关设置 实例173 获得Windows和System的路径 实例174 控制...
log4cplusplus 基于 log4cplus 封装的自用的 C++ 日志库 - 线程安全 - 支持异步 - 支持中文路径和内容 - 支持输出到文件、控制台、调试器 - 支持格式化打印
cc实例169 格式化磁盘 6.3 桌面相关设置 cc实例170 隐藏.c显示“开始”按钮 cc实例171 隐藏.c显示桌面文件 cc实例172 隐藏.c显示Windows任务栏 6.4 系统相关设置 cc实例173 获得...
cc实例165 判断驱动器属性 cc实例166 获取磁盘空间信息 cc实例167 获取磁盘序列号 cc实例168 取消磁盘共享 cc实例169 格式化磁盘 6.3 桌面相关设置 cc实例170 隐藏.c显示“开始”按钮 cc实例171 隐藏...
C++Builder精彩编程实例集锦的源代码(4,5,6部分): 第四部分 图像处理 实例124 如何浏览图像 实例125 如何滚动浏览大图像 实例126 如何拖动图像 实例127 如何动态显示图像 实例128 如何维护多个图像 实例129 ...
cc实例169 格式化磁盘 6.3 桌面相关设置 cc实例170 隐藏.c显示“开始”按钮 cc实例171 隐藏.c显示桌面文件 cc实例172 隐藏.c显示Windows任务栏 6.4 系统相关设置 cc实例173 获得...
实例173 如何为计算机增加启动日志 实例174 如何获取操作系统的启动模式 实例175 如何变暗屏幕颜色 实例176 如何关闭计算机 实例177 如何禁止关闭Windows操作系统 实例178 如何修改IE浏览器的背景 实例179 ...
c#源码转java源码的 EasyLogger 1. 介绍 是一款超轻量级(ROM<1.6K, RAM<0.3K)、高性能的 ...1、RAW格式:未经过格式化的原始日志。 2、标签:在软件中可以按照文件、模块、功能等方面,对需要打印的
实例169 格式化磁盘 6.3 桌面相关设置 实例170 隐藏、显示“开始”按钮 实例171 隐藏、显示桌面文件 实例172 隐藏、显示Windows任务栏 6.4 系统相关设置 实例173 获得Windows和System的路径 实例174 控制...
实例173 如何为计算机增加启动日志 实例174 如何获取操作系统的启动模式 实例175 如何变暗屏幕颜色 实例176 如何关闭计算机 实例177 如何禁止关闭Windows操作系统 实例178 如何修改IE浏览器的背景 实例179 ...
随着公司的发展扩大,需要一套结构化的方式管理和保存事件日志。 集播放、音效、转换、歌词等众多功能于一身。 ppt文件格式转换器它采用了多点对多点的原理,一般简称 BT(BitTorrent) 也就是大家所说的变态下载。是...
很简单,只能播放mp3格式的音乐。 功能: --------------------------------------- --------关闭-- 打开----最小化------- --------------------------------------- 显示歌曲名字 -------------------- -------...