一个超级简单的c++日志文件(c++11实现,跨平台)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一个超级简单的c++日志文件(c++11实现,跨平台),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3281字,纯文字阅读大概需要5分钟。
内容图文
![一个超级简单的c++日志文件(c++11实现,跨平台)](/upload/InfoBanner/zyjiaocheng/851/4803c6dfa76c4118967d40695c5c3727.jpg)
myLog.h
#ifndef __myLog_H_
#define __myLog_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <ratio>
#include <chrono>
#include <mutex>
std::string GetName(char * fileName);
std::string getCurrentSystemTime();
// 初始化路径"sdcard/log.txt" 或者 "c:\\work\\log.txt"
int LOGINIT(char *path);
// 结束时候调用
int LOGEnd();
// 设置最大日志文件,默认5M, mSize单位是字节
int LOGSetMaxSpace(int mSize);
int logWrite(char * s);
#define LOGE(...) { char temp[8192] = {0}; sprintf(temp, "%s", getCurrentSystemTime().c_str()); sprintf(temp+strlen(temp), " %s %d ", GetName(__FILE__).c_str(), __LINE__); sprintf(temp+strlen(temp), __VA_ARGS__); sprintf(temp + strlen(temp), "\n"); printf("%s", temp); logWrite(temp); }
#endif
myLog.cpp
#include "myLog.h"
std::mutex mux;
static FILE * fp = NULL;
int MaxLenChar = 1024 * 1024 * 5; // 5 M 数据
int LOGINIT(char *path)
{
fp = fopen(path, "w+");
if (fp == NULL)
{
return -1;
}
return 0;
}
int LOGSetMaxSpace(int mSize)
{
MaxLenChar = mSize;
return 0;
}
int logWrite(char * s)
{
if (fp)
{
int len = ftell(fp);
if (len > MaxLenChar)
{
LOGEnd();
return 0;
}
mux.lock();
fprintf(fp, "%s", s);
fflush(fp);
mux.unlock();
}
return 0;
}
int LOGEnd()
{
if (fp)
{
fflush(fp);
fclose(fp);
fp = 0;
}
return 0;
}
std::string getCurrentSystemTime()
{
auto time_now = std::chrono::system_clock::now();
auto tt = std::chrono::system_clock::to_time_t(time_now);
auto duration_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now.time_since_epoch());
auto duration_in_s = std::chrono::duration_cast<std::chrono::seconds>(time_now.time_since_epoch());
int theMs = duration_in_ms.count() - duration_in_s.count() * 1000;
struct tm* ptm = localtime(&tt);
char date[60] = { 0 };
sprintf(date, "%d-%02d-%02d-%02d.%02d.%02d.%03d%",
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec, theMs);
return std::string(date);
}
std::string GetName(char * fileName)
{
char temp[256] = { 0 };
memcpy(temp, fileName, strlen(fileName) + 1);
std::string sName = temp;
#ifdef WIN32
int startP = sName.find_last_of("\\");
if (startP == -1)
{
startP = 0;
}
std::string theLastName = sName.substr(startP + 1, sName.size() - startP);
#else
int startP = sName.find_last_of("/");
if (startP == -1)
{
startP = 0;
}
std::string theLastName = sName.substr(startP + 1, sName.size() - startP);
#endif
return theLastName;
}
main.cpp
#include<stdio.h>
#include "myLog.h"
int main()
{
LOGINIT("./log.txt");
for (int i = 0; i < 1000; i++)
{
LOGE("%f .....%d ", 12563.0 * i, i);
}
LOGEnd();
getchar();
return 0;
}
输出log.txt
2018-10-17-19.24.27.155 main.cpp 10 0.000000 …0
2018-10-17-19.24.27.158 main.cpp 10 12563.000000 …1
2018-10-17-19.24.27.158 main.cpp 10 25126.000000 …2
2018-10-17-19.24.27.159 main.cpp 10 37689.000000 …3
2018-10-17-19.24.27.159 main.cpp 10 50252.000000 …4
2018-10-17-19.24.27.159 main.cpp 10 62815.000000 …5
2018-10-17-19.24.27.159 main.cpp 10 75378.000000 …6
2018-10-17-19.24.27.159 main.cpp 10 87941.000000 …7
2018-10-17-19.24.27.159 main.cpp 10 100504.000000 …8
2018-10-17-19.24.27.159 main.cpp 10 113067.000000 …9
2018-10-17-19.24.27.159 main.cpp 10 125630.000000 …10
内容总结
以上是互联网集市为您收集整理的一个超级简单的c++日志文件(c++11实现,跨平台)全部内容,希望文章能够帮你解决一个超级简单的c++日志文件(c++11实现,跨平台)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。