PageRenderTime 253ms CodeModel.GetById 237ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/TGame/TUtil/Log/LogUtil.cpp

http://awoe.googlecode.com/
C++ | 135 lines | 112 code | 21 blank | 2 comment | 10 complexity | 02ff39066cf67b7a44114df24634c804 MD5 | raw file
  1
  2#include "stdafx.h"
  3#include "Logutil.h"
  4#include <vector>
  5#include <string>
  6#include <cstring>
  7#include <strsafe.h>
  8#include <fstream>
  9#include <time.h>
 10
 11
 12namespace woe
 13{
 14		TimeStamp TimeStamp::_startStamp;
 15
 16		TimeStamp::TimeStamp() 
 17		{
 18#ifdef WIN32
 19			//
 20			//	time is not accurate use win32 api later
 21			_seconds = (int)::time(NULL);
 22			_microSeconds = 0;
 23#else
 24			struct timeval tv;
 25			::gettimeofday(&tv, NULL);
 26			_seconds = tv.tv_sec;
 27			_microSeconds = tv.tv_usec;
 28#endif
 29		}
 30
 31		TimeStamp::TimeStamp(unsigned int seconds, unsigned int microSeconds) :
 32		_seconds(seconds),
 33			_microSeconds(microSeconds) 
 34		{
 35		}
 36
 37
 38		void TimeStamp::format(std::string& fmt)const
 39		{
 40			struct std::tm currentTime;
 41			time_t t = getSeconds();
 42			localtime_s(&currentTime, &t);
 43			char formatted[100];
 44			StringCbPrintf( formatted, 
 45				100,
 46				TS("%04d-%02d-%02d %02d:%02d:%02d"), 
 47				currentTime.tm_year +1900, 
 48				currentTime.tm_mon +1,
 49				currentTime.tm_mday, 
 50				currentTime.tm_hour, 
 51				currentTime.tm_min, 
 52				currentTime.tm_sec );
 53			fmt = formatted;
 54		}
 55
 56		const string& getLogLevelName(short lvl)
 57		{
 58			static std::vector<string> __log_level_names;
 59			static string __default_name = TS("unknown");
 60			if (__log_level_names.empty())
 61			{
 62				__log_level_names.push_back(TS("debug"));
 63				__log_level_names.push_back(TS("info"));
 64				__log_level_names.push_back(TS("warn"));
 65				__log_level_names.push_back(TS("error"));
 66				__log_level_names.push_back(TS("fatal"));
 67			}
 68
 69			if (lvl>=0 && lvl<(int)__log_level_names.size())
 70			{
 71				return __log_level_names[lvl];
 72			}
 73			else
 74			{
 75				return __default_name;
 76			}
 77		}
 78
 79
 80
 81#ifdef UNICODE
 82#define		atoint	_wtoi
 83#else
 84#define		atoint	atoi
 85#endif
 86
 87		
 88
 89		std::vector<string> LogLevelUtil::__log_level_names;
 90
 91		const string& LogLevelUtil::getLogLevelName(short lvl)
 92		{
 93
 94			static string __default_name = TS("unknown");
 95			if (__log_level_names.empty())
 96			{
 97				__log_level_names.push_back(TS("debug"));
 98				__log_level_names.push_back(TS("info"));
 99				__log_level_names.push_back(TS("warn"));
100				__log_level_names.push_back(TS("error"));
101				__log_level_names.push_back(TS("fatal"));
102			}
103
104			if (lvl>=0 && lvl<(int)__log_level_names.size())
105			{
106				return __log_level_names[lvl];
107			}
108			else
109			{
110				return __default_name;
111			}
112		}
113
114		short	LogLevelUtil::getLogLevel(const string& lvlName)
115		{
116			if (__log_level_names.empty())
117			{
118				__log_level_names.push_back(TS("debug"));
119				__log_level_names.push_back(TS("info"));
120				__log_level_names.push_back(TS("warn"));
121				__log_level_names.push_back(TS("error"));
122				__log_level_names.push_back(TS("fatal"));
123			}
124
125			for (size_t i=0; i<__log_level_names.size(); i++)
126			{
127				if (lvlName == __log_level_names[i])
128				{
129					return short(i);
130				}
131			}
132
133			return 0;
134		}
135}