Andy Niu �����ĵ�

Andy Niu

Andy Niu Help  1.0.0.0
CPP工具

模块

 Rapidxml
 

变量

跨平台定时器
 
 我的日志
 
 控制台进度条
 

详细描述

变量说明

我的日志

头文件

#ifndef NIU_LOG_H_
#define NIU_LOG_H_

#ifdef DLL_FILE
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

#include <stdarg.h>
#include <string>
#include <windows.h>

using namespace std;

#define LOG_DEBUG   1
#define LOG_INFO    2
#define LOG_WARN    4
#define LOG_ERROR   8
#define LOG_FATAL   16

#define OUTPUT_STD      1
#define OUTPUT_CONSOLE  2


class DLL_API NiuLog
{
public:
    NiuLog();
public:
    void Debug(const char* file,const char* func,int lineNum,const char* format, ...);
    void Info (const char* file,const char* func,int lineNum,const char* format, ...);
    void Warn (const char* file,const char* func,int lineNum,const char* format, ...);
    void Error(const char* file,const char* func,int lineNum,const char* format, ...);
    void Fatal(const char* file,const char* func,int lineNum,const char* format, ...);

    void SetLevel(int level);
    void GetLevel(int& level);

    void SetOutput(int output);
    void GetOutput(int& output);

    inline void SetConsoleColor(WORD colorValue);

private:
    inline string getNowTimeAsString();
    
private:
    int     _level;
    int     _output;
    HANDLE  _consoleHandle;
};

#define LogDebug(log,format,...) do\
{\
    log.Debug(__FILE__, __FUNCTION__, __LINE__,format,##__VA_ARGS__);\
}while(0)

#define LogInfo(log,format,...) do\
{\
    log.Info(__FILE__, __FUNCTION__, __LINE__,format,##__VA_ARGS__);\
}while(0)

#define LogWarn(log,format,...) do\
{\
    log.Warn(__FILE__, __FUNCTION__, __LINE__,format,##__VA_ARGS__);\
}while(0)

#define LogError(log,format,...) do\
{\
    log.Error(__FILE__, __FUNCTION__, __LINE__,format,##__VA_ARGS__);\
}while(0)

#define LogFatal(log,format,...) do\
{\
    log.Fatal(__FILE__, __FUNCTION__, __LINE__,format,##__VA_ARGS__);\
}while(0)

#endif

源代码

#include "niu_log.h"

#include <stdio.h>
#include <time.h>


#define LOG(x) do\
{\
    if((_output & (OUTPUT_STD+OUTPUT_CONSOLE)) == 0)\
    {\
        return;\
    }\
    if((_level & LOG_##x) == 0)\
    {\
        return;\
    }\
    char buf[2048] = {0};\
    sprintf(buf,"[" #x " %s %s:%s:%d] ",\
        this->getNowTimeAsString().c_str(),\
        file,\
        func,\
        lineNum);\
    char* p = buf+strlen(buf);\
    va_list ap;\
    va_start(ap, format);\
    p += _vsnprintf(p, sizeof(buf) - 1, format, ap);\
    va_end(ap);\
    int len = strlen(buf);\
    buf[len] = '\r';\
    buf[len+1] = '\n';\
    if((_output & OUTPUT_STD) >0)\
    {\
        printf(buf);\
    }\
    if((_output & OUTPUT_CONSOLE) >0)\
    {\
        ::OutputDebugStringA(buf);\
    }\
} while (0)


NiuLog::NiuLog()
{
    _consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
}

void NiuLog::Debug(const char* file,const char* func,int lineNum,const char* format, ...)
{
    SetConsoleColor(FOREGROUND_GREEN);
    LOG(DEBUG);
}

void NiuLog::Info(const char* file,const char* func,int lineNum,const char* format, ...)
{
    SetConsoleColor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    LOG(INFO);
}

void NiuLog::Warn(const char* file,const char* func,int lineNum,const char* format, ...)
{
    SetConsoleColor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    LOG(WARN);
}

void NiuLog::Error(const char* file,const char* func,int lineNum,const char* format, ...)
{
    SetConsoleColor(FOREGROUND_RED|FOREGROUND_INTENSITY);
    LOG(ERROR);
}

void NiuLog::Fatal(const char* file,const char* func,int lineNum,const char* format, ...)
{
    SetConsoleColor(FOREGROUND_BLUE|FOREGROUND_INTENSITY);
    LOG(FATAL);
}

void NiuLog::SetLevel(int level)
{
    _level = level;
}

void NiuLog::SetOutput(int output)
{
    _output = output;
}

string NiuLog::getNowTimeAsString()
{
    char buf[64] = {0};
    time_t now = time(NULL);
    //方法1:
    /*tm* ptm = localtime(&now);
    sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", 
        ptm->tm_year+1900, 
        ptm->tm_mon+1, 
        ptm->tm_mday,
        ptm->tm_hour,
        ptm->tm_min, 
        ptm->tm_sec);*/

    //方法2:
    strftime(buf, 64, "%Y-%m-%d %H:%M:%S",localtime(&now));
    return buf;
}


void NiuLog::SetConsoleColor(WORD colorValue)  
{  
    if (_consoleHandle != INVALID_HANDLE_VALUE)
    {
        SetConsoleTextAttribute(_consoleHandle, colorValue); 
    }   
}  

测试

#include "niu_log.h"
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    NiuLog log;
    log.SetLevel(LOG_DEBUG|LOG_INFO|LOG_WARN|LOG_ERROR|LOG_FATAL);
    log.SetOutput(OUTPUT_STD|OUTPUT_CONSOLE);

    LogDebug(log,"This is Debug");
    LogInfo (log,"This is Info");
    LogWarn (log,"This is Warn");
    LogError(log,"This is Error");
    LogFatal(log,"This is Fatal");

    getchar();
    return 0;
}
控制台进度条
int len = 50;
string str ="";
for(int i=0; i<len;++i)
{
    str+="|";
}
log.SetConsoleColor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
printf("start...\n");
printf("%s\r",str.c_str()); // 打印竖线,然后回到行首

str ="";
for(int i=0; i<len;++i)
{
    str+=">";
}
log.SetConsoleColor(FOREGROUND_GREEN|FOREGROUND_INTENSITY);

for (int i = 0; i < len; ++i)  
{  
    putchar(str[i]);  
    Sleep(200);  
}  
putchar('\n');
printf("end\n");
C 跨平台定时器

1、头文件

    #ifndef TIMER_H_
    #define TIMER_H_
    
    typedef void (*TimerTick)(void* param);
    
    struct TimerInfo
    {
        TimerTick _TimerTick;
        void*     _Param;
        int       _Start;
        int       _Interval;
    
        TimerInfo(TimerTick timerTick,void* param,int start,int interval)
        {
            _TimerTick = timerTick;     
            _Param     = param;
            _Start     = start;
            _Interval  = interval;
        }
    };
    
    class Timer
    {
    public:
        Timer(TimerTick timerTick,void* param,int start,int interval);
    };
#endif

2、实现

    #include "timer.h"
    #ifdef WIN32
    #include <windows.h>
    #else
    #include <pthread.h>
    #include <unistd.h>
    #endif
    
    void NiuSleep(int ms)
    {
        #ifdef WIN32
            ::Sleep(ms);
        #else
            ::usleep(ms*1000);
        #endif
    }
    
    
    #ifdef WIN32
    DWORD WINAPI ThreadRun(PVOID param)
    #else
    void* ThreadRun(void* param)
    #endif
    {
        TimerInfo* timerInfo = (TimerInfo*)param;
        NiuSleep(timerInfo->_Start);
    
        while(true)
        {
            timerInfo->_TimerTick(timerInfo->_Param);
            NiuSleep(timerInfo->_Interval);
        }
        
        return 0;
    }
    
    Timer::Timer(TimerTick timerTick,void* param,int start,int interval)
    {
        TimerInfo* timerInfo = new TimerInfo(timerTick,param,start,interval);
    
        #ifdef WIN32
        HANDLE hThread = CreateThread(NULL,0,ThreadRun,timerInfo,0,NULL);
        CloseHandle(hThread);
        #else
        pthread_t hThread;
        pthread_create(&hThread,NULL,ThreadRun,(void*)timerInfo);
        #endif
    }

3、测试代码

    #include "timer.h"
    #include <stdio.h>
    
    void tick(void* param)
    {
        printf("haha\n");
    }
    
    int main(int argc,char* argv[])
    {
        Timer timer(tick,NULL,5000,1000);
    
        getchar();
        return 0;
    }

4、编译测试

    [root@localhost Timer]# ll
    total 12
    -rw-r--r-- 1 root root 181 Jan  7 17:17 main.cpp
    -rw-r--r-- 1 root root 823 Jan  7 17:15 timer.cpp
    -rw-r--r-- 1 root root 441 Jan  7 16:47 timer.h
    
    [root@localhost Timer]# g++ -c timer.cpp
    In file included from timer.cpp:1:
    timer.h:28:7: warning: no newline at end of file
    timer.cpp:48:2: warning: no newline at end of file
    
    [root@localhost Timer]# g++ -c main.cpp
    In file included from main.cpp:1:
    timer.h:28:7: warning: no newline at end of file
    main.cpp:16:2: warning: no newline at end of file
    
    [root@localhost Timer]# g++ -o main main.o timer.o -lpthread
    
    [root@localhost Timer]# ll
    total 28
    -rwxr-xr-x 1 root root 6485 Jan  7 17:23 main
    -rw-r--r-- 1 root root  181 Jan  7 17:17 main.cpp
    -rw-r--r-- 1 root root 1308 Jan  7 17:23 main.o
    -rw-r--r-- 1 root root  823 Jan  7 17:15 timer.cpp
    -rw-r--r-- 1 root root  441 Jan  7 16:47 timer.h
    -rw-r--r-- 1 root root 1856 Jan  7 17:23 timer.o
    
    [root@localhost Timer]# ./main
    haha
    haha
    haha
Copyright (c) 2015~2016, Andy Niu @All rights reserved. By Andy Niu Edit.