一个简单的log
#pragma once
#include <windows.h>
#include <process.h>
class CLogger
{
public:
static CRITICAL_SECTION m_cs;//保证所有的对象都能访问到它
public:
CLogger();
~CLogger();
static int logger_to_file(const char* logfile);//加锁,放锁
static int logger(const char* fmt, ...);
static int close_logger_file();
private:
static int log_ref;
static FILE* logfp; }; // namespace Logger
// {
// int logger_to_file(const char* logfile);
// int logger(const char* fmt, ...);
// int close_logger_file(); // int log_ref;
// FILE* logfp;
// } #pragma once
#include "stdafx.h"
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
#include "log.h"
using namespace std; int CLogger::log_ref = 0;
FILE* CLogger::logfp = NULL;
std::string get_current_time();
CRITICAL_SECTION CLogger::m_cs = {};
CLogger::CLogger()
{ }
CLogger::~CLogger()
{
close_logger_file();
}
int CLogger::logger_to_file(const char* logfile)
{
if (log_ref++ == 0) //保证只有为0打开,与关闭对应起来
{
InitializeCriticalSection(&m_cs);
logfp = fopen(logfile, "w+b");
if (!logfp)
{
log_ref--;
return -1;
}
}
return 0;
} int CLogger::logger(const char* fmt, ...)
{
EnterCriticalSection(&m_cs);
static char buffer[10240];
va_list va;
int ret = 0; va_start(va, fmt);
vsprintf(buffer, fmt, va); std::string time = get_current_time(); // 输出到文件.
if (logfp)
{
fprintf(logfp, "[%s] %s", time.c_str(), buffer);
fflush(logfp);
} // 输出到屏幕.
ret = printf("[%s] %s", time.c_str(), buffer); va_end(va); EnterCriticalSection(&m_cs);
return ret;
} int CLogger::close_logger_file()
{
if (!logfp)
return -1; if (--log_ref == 0)
{
fclose(logfp);
logfp = NULL;
DeleteCriticalSection(&m_cs);
} return 0;
} std::string get_current_time()
{
char buffer[1024] = {0};
std::string ret;
struct tm curr_time;
time_t tmp_time; time(&tmp_time); curr_time = *(localtime(&tmp_time)); if (curr_time.tm_year > 50)
{
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
curr_time.tm_year + 1900, curr_time.tm_mon + 1, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec);
}
else
{
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
curr_time.tm_year + 2000, curr_time.tm_mon + 1, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec);
} return std::string(buffer);
}
一个简单的log的更多相关文章
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》
Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
- 实现一个简单的Log框架
实际上算不上框架,只是自己对日志框架的一点理解. 核心接口:Logger,供调用者完成不同等级的日志输出 package com.lichmama.log.service; public interf ...
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...
- 我的Android进阶之旅------>Android关于Log的一个简单封装
android.util.Log类,可以方便地用于在编码调试过程中打印日志.但是在发布后的产品中,如果有太多的日志打印,则会严重地影响性能.对android.util.Log类做一个简单的封装,当产品 ...
- 我的Android进阶之旅------>Android关于Log的一个简单封装
android.util.Log类,能够方便地用于在编码调试过程中打印日志. 可是在公布后的产品中,假设有太多的日志打印.则会严重地影响性能. 对android.util.Log类做一个简单的封装.当 ...
- 使用Servlet和JSP实现一个简单的Web聊天室系统
1 问题描述 利用Java EE相关技术实现一个简单的Web聊天室系统,具体要求如下. (1)编写一个登录 ...
随机推荐
- IOS下自定义click事件使用alert引发的血案
使用过iscroll插件的同学都知道iscroll支持自定义事件,即在调用iscroll时参数赋值options.click = true. 接下来定义事件如: $clinicAppoint.on(' ...
- Envelope Letter
http://www.thefullwiki.org/More_C%2B%2B_Idioms/Envelope_Letter http://www.smallmemory.com/almanac/Co ...
- 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
1.适用范围:一些很小的提示或警告音频. 2.使用限制: 声音长度不能超过30秒 声音文件必须是PCM或IMA4(IMA/ADPCM)格式.(有时候可播放一些特殊的.mp3) 打包成.caf..aif ...
- top命令详解(转,详细)
来源:脚本之家(http://www.jb51.net/article/40807.htm) 本文通过一个运行中的WEB服务器的top监控截图,讲述top视图中的各种数据的含义,还包括视图中各进程(任 ...
- Database Initialization Strategies in Code-First:
You already created a database after running your Code-First application the first time, but what ab ...
- 开源项目管理平台*redmine*的架设
yum -y install ruby yum install rubygems gem install heroku gem install rack -v=1.0.1 gem install ru ...
- 密钥文件snk
1.(what)是什么? 由一个程序集的标识组成并通过公钥和数字签名(针对该程序集生成)加强的名称,其中的标识包括程序集的简单文 本名称.版本号和区域性信息(如果提供的话). 2.(why)为什么 ...
- 该不该用inline-block取代float? inline和float的区别?
该不该用inline-block取代float? 请看这篇文章引用: jtyjty99999的博客 让块级元素 水平排列的通常方式是float, 但是float可能会带来很多意外的问题 可以考虑用in ...
- thinkphp-许愿墙-2
在数组中,也可以使用函数,如: $data = array( 'username'=> I('username','', 'htmlspecailchars'), 'content'=> ...
- [译]git fetch
git fetch从远程仓储导入commit到你的本地仓储. 这些fetch到的commit是做为一个远程分支存储在你本地的. 这样你可以在集成这些commit到你的项目前先看看都有些什么修改. 用法 ...