日志加入时间信息

mapping_mei 2007-01-16 04:07:36

目前的日志没有时间信息,要想在日志的每一行前面显示生成日志时间,如何加入时间信息?
下面是Logging functions,请高手指点下思路:
void CServiceModule::LogEvent(LPCTSTR pFormat, ...)
{
TCHAR chMsg[256];
HANDLE hEventSource;
LPTSTR lpszStrings[1];
va_list pArg;

va_start(pArg, pFormat);
_vstprintf(chMsg, pFormat, pArg);
va_end(pArg);

lpszStrings[0] = chMsg;

if (m_bService)
{
/* Get a handle to use with ReportEvent(). */
hEventSource = RegisterEventSource(NULL, m_szServiceName);
if (hEventSource != NULL)
{
/* Write to event log. */
ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL);
DeregisterEventSource(hEventSource);
}
}
else
{
// As we are not running as a service, just write the error to the console.
_putts(chMsg);
}
}
...全文
214 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sco_field 2007-01-16
  • 打赏
  • 举报
回复
up pottichu??
what's mean?
=============
就是我顶你的肺!
============
应该是 我顶你个肺啊~要专业~^_^
Sco_field 2007-01-16
  • 打赏
  • 举报
回复
要是MFC的话应该可以直接用GetCurrentTime吧
pottichu 2007-01-16
  • 打赏
  • 举报
回复
表顶我的肺. -_-#
HewpKanXue 2007-01-16
  • 打赏
  • 举报
回复
up pottichu??
what's mean?
=============
就是我顶你的肺!
mapping_mei 2007-01-16
  • 打赏
  • 举报
回复
up pottichu??
what's mean?
OOPhaisky 2007-01-16
  • 打赏
  • 举报
回复
up pottichu^_^
iu_81 2007-01-16
  • 打赏
  • 举报
回复
LPSYSTEMTIME lpSystemTime = new SYSTEMTIME;
GetLocalTime (lpSystemTime);
Log::Show("Test date:%d-%d-%d time:%d:%d:%d\n",lpSystemTime->wYear,
lpSystemTime->wMonth,
lpSystemTime->wDay,
lpSystemTime->wHour,
lpSystemTime->wMinute,
lpSystemTime->wSecond);
pottichu 2007-01-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <time.h>

char* getDateTimeStr(char *str, int len) {
time_t long_time;
struct tm t;
time(&long_time);
t = *localtime(&long_time);
_snprintf(str, len, "%04d-%02d-%02d %02d:%02d:%02d",
t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
return str;
}

int main()
{
char tim[128] = {0};
getDateTimeStr(tim, 128);
printf("%s \n", tim);
}
pottichu 2007-01-16
  • 打赏
  • 举报
回复
char* Tools::getDateTimeStr(char *str, int len) {
time_t long_time;
struct tm t;
time(&long_time);
t = *localtime(&long_time);
_snprintf(str, len, "%04d-%02d-%02d %02d:%02d:%02d",
t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
return str;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧