调整ACE日志时间输出格式

36 篇文章 0 订阅
9 篇文章 1 订阅
ACE日志输出时用%D输出时间:
ACE_DEBUG ((LM_INFO, ACE_TEXT (" %D Hi Mom/n")));
结果如:
Thu Mar 13 2008 17:38:01.453000Hi Mom
 
格式不符合中文习惯,并且太长。我把源码改了一下,这样输出时间时符合中国习惯(比如: 08-05-12 09:37:20.123),并且节省输出长度( Windows下只能输出到毫秒,所以秒后面只要3位小数即可),改完重新编译ACE库即可。
 
 
// ACE.cpp,v 4.357 2003/11/05 23:30:46 shuston Exp
 
#include "ace/ACE.h"
 
#if defined ( ACE_LACKS_INLINE_FUNCTIONS )
#include "ace/ACE.i"
#endif /* ACE_LACKS_INLINE_FUNCTIONS */
 
… …
 
// Returns the current timestamp in the form
// "hour:minute:second:microsecond." The month, day, and year are
// also stored in the beginning of the date_and_time array.
 
ACE_TCHAR *
ACE :: timestamp ( ACE_TCHAR date_and_time [],
                int date_and_timelen ,
                int return_pointer_to_first_digit )
{
  //ACE_TRACE ("ACE::timestamp");
 
 if ( date_and_timelen < 35)
    {
      errno = EINVAL ;
      return 0;
    }
 
#if defined ( WIN32 )
   // Emulate Unix. Win32 does NOT support all the UNIX versions
   // below, so DO we need this ifdef.
 static const ACE_TCHAR * day_of_week_name [] =
 {
     ACE_LIB_TEXT ( "Sun" ),
    ACE_LIB_TEXT ( "Mon" ),
    ACE_LIB_TEXT ( "Tue" ),
    ACE_LIB_TEXT ( "Wed" ),
    ACE_LIB_TEXT ( "Thu" ),
    ACE_LIB_TEXT ( "Fri" ),
    ACE_LIB_TEXT ( "Sat" )
 };
 
 static const ACE_TCHAR * month_name [] =
 {
    ACE_LIB_TEXT ( "Jan" ),
    ACE_LIB_TEXT ( "Feb" ),
    ACE_LIB_TEXT ( "Mar" ),
    ACE_LIB_TEXT ( "Apr" ),
    ACE_LIB_TEXT ( "May" ),
    ACE_LIB_TEXT ( "Jun" ),
    ACE_LIB_TEXT ( "Jul" ),
    ACE_LIB_TEXT ( "Aug" ),
    ACE_LIB_TEXT ( "Sep" ),
    ACE_LIB_TEXT ( "Oct" ),
    ACE_LIB_TEXT ( "Nov" ),
    ACE_LIB_TEXT ( "Dec" )
 };
 
 SYSTEMTIME local ;
 :: GetLocalTime (& local );
 
  /* 更改以下两句,输出 08-05-12 09:37:20.123 的格式
 ACE_OS::sprintf (date_and_time,
                   ACE_LIB_TEXT ("%3s %3s %2d %04d %02d:%02d:%02d.%06d"),
                   day_of_week_name[local.wDayOfWeek],
                   month_name[local.wMonth - 1],
                   (int) local.wDay,
                   (int) local.wYear,
                   (int) local.wHour,
                   (int) local.wMinute,
                   (int) local.wSecond,
                   (int) (local.wMilliseconds * 1000));
 return &date_and_time[15 + (return_pointer_to_first_digit != 0)*/
 ACE_OS:: sprintf ( date_and_time ,
      ACE_LIB_TEXT ( "%02d-%02d-%02d %02d:%02d:%02d.%03d" ),
      ( int ) local . wYear - 2000,
      ( int ) local . wMonth ,
      ( int ) local . wDay ,
      ( int ) local . wHour ,
      ( int ) local . wMinute ,
      ( int ) local . wSecond ,
      ( int ) local . wMilliseconds );
 return & date_and_time [9];
#else   /* UNIX */
  ACE_TCHAR timebuf [26]; // This magic number is based on the ctime(3c) man page.
  ACE_Time_Value cur_time = ACE_OS:: gettimeofday ();
  time_t secs = cur_time . sec ();
 
 ACE_OS:: ctime_r (& secs ,
                   timebuf ,
                   sizeof timebuf );
  // date_and_timelen > sizeof timebuf!
 ACE_OS:: strsncpy ( date_and_time ,
                    timebuf ,
                     date_and_timelen );
  char yeartmp [5];
 ACE_OS:: strsncpy ( yeartmp ,
                    & date_and_time [20],
                    5);
  char timetmp [9];
 ACE_OS:: strsncpy ( timetmp ,
                    & date_and_time [11],
                    9);
 ACE_OS:: sprintf (& date_and_time [11],
                   "%s %s.%06ld" ,
                   yeartmp ,
                   timetmp ,
                   cur_time . usec ());
  date_and_time [33] = '/0' ;
 return & date_and_time [15 + ( return_pointer_to_first_digit != 0)];
#endif /* WIN32 */
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值