Timer-logs recorded per second

Source: Internet
Author: User
1 # include <iostream> 2 # include <windows. h> 3 # include <MMSystem. h> 4 # pragma comment (lib, "winmm. lib ") 5 # include <time. h> 6 # include <stdint. h> 7 using namespace std; 8 9 # define MAX_LOG_FILE_SIZE 0x4000000 // 64MB10 11 class CTimer {12 public: 13 CTimer () 14 {15 m_tickCount = 0; 16} 17 ~ CTimer () 18 {19 if (m_timerID) 20 {21 timeKillEvent (m_timerID); 22} 23} 24 static void CALLBACK TimerCallBack (UINT uTimeID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2); 25 void OnTimer (); 26 void Log (const char * fmt ,...); 27 28 protected: 29 void Call () 30 {31 Log ("tick... % d ", m_tickCount); 32 ++ m_tickCount; 33} 34 void print_format_time (FILE * fileName); 35 36 private: 37 MMRESULT m_timerID; 38 int m_tickCount ; 39}; 40 41 void CALLBACK CTimer: TimerCallBack (UINT uTimeID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) 42 {43 CTimer * _ ctimer = (CTimer *) dwUser; 44 _ ctimer-> Call (); 45} 46 47 void CTimer: OnTimer () 48 {49 timeSetEvent (1000,1, (LPTIMECALLBACK) TimerCallBack, (DWORD) this, TIME_PERIODIC); 50} 51 52 void CTimer: print_format_time (FILE * fileName) 53 {54 SYSTEMTIME _ effecime; 55 GetLocalTime (& _ effecime); 56 57 Fprintf (fileName, "% 04d-% 02d-% 02d % 02d: % 02d: % 02d. % 06d ", _ policime. wYear, 58 _ invalid IME. wMonth, _ required IME. wDay, _ policime. wHour, _ javasime. wMinute, _ policime. wSecond, _ policime. wMilliseconds); 59} 60 61 void CTimer: Log (const char * fmt ,...) 62 {63 static int file_no (0); 64 static FILE * log_fp = NULL; 65 if (log_fp = NULL) 66 {67 char log_name [64]; 68 uint32_t pid; 69 pid = (uint32_t) GetCurrentProcessId (); 70 sprintf_s (lo G_name, 64, "log_cmdd_cmdd.txt", pid, file_no); 71 fopen_s (& log_fp, log_name, "w"); 72 if (! Log_fp) 73 return; 74} 75 print_format_time (log_fp); 76 77 va_list ap; 78 va_start (ap, fmt); 79 vfprintf_s (log_fp, fmt, ap ); 80 va_end (ap); 81 fflush (log_fp); 82 fprintf (log_fp, "\ n"); 83 if (ftell (log_fp)> MAX_LOG_FILE_SIZE) 84 {85 fclose (log_fp); 86 log_fp = NULL; 87 ++ file_no; 88} 89 90} 91 92 int main (int argc, char * argv []) 93 {94 CTimer _ timer; 95 _ timer. onTimer (); 96 system ("pause"); 97 return 0; 98}

 

Timer-logs recorded per second

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.