適当な関数の一連の処理のログを出力するコードをC++で書いています。
ログメッセージをファイル(.log)に出力する際に、現在時刻を付随させたいのですが
ファイル内にコード上で取得した時刻を書き込む方法がわかりません。
構造体から得た情報を文字列に変換する、という処理が必要なのかと考察してみましたが、
その方法がわかりませんでした。
関数名 void outputCurrentTime() と void writeLog(string LogMessage)
をどのように訂正すれば良いかご教授願いたいです。
以下がソースコードになります。
おかしい点など、ご指摘ください。よろしくお願いいたします。
C++
1#include <iostream> 2#include <string> 3#include <fstream> 4#include <sstream> 5#include <windows.h> 6using namespace std; 7 8string gLogMessage = "ログファイル出力開始"; 9 10void outputCurrentTime() 11{ 12 SYSTEMTIME UTC; 13 SYSTEMTIME Local; 14 TIME_ZONE_INFORMATION getTimezone; 15 GetTimeZoneInformation( &getTimezone ); 16 GetSystemTime( &UTC ); 17 SystemTimeToTzSpecificLocalTime( &getTimezone, &UTC, &Local ); 18 19 ofstream outputLog; 20 outputLog << Local.wYear << "/" << Local.wYear << "/" << Local.wMonth << "/" 21 << Local.wDay << "/" << Local.wHour << ":" << Local.wMinute << ":" 22 << Local.wSecond << ":" << Local.wMilliseconds; 23} 24 25void writeLog( string strLogMessage ) 26{ 27 outputCurrentTime(); 28 29 gLogMessage += strLogMessage; 30} 31 32void swap( int *pNum1, int *pNum2 ) 33{ 34 writeLog( "->call->swap\n" ); 35 36 //pNum1 = NULL; // (異常系を通る場合の確認用) 37 //pNum2 = NULL; 38 39 if( NULL != pNum1 40 && NULL != pNum2 ){ 41 if( *pNum1 > *pNum2 ){ 42 int nTmp = *pNum1; 43 *pNum1 = *pNum2; 44 *pNum2 = nTmp; 45 writeLog( "->Number was swaped.\n" ); 46 } 47 writeLog( "->end->swap\n" ); 48 } 49 else{ 50 writeLog( "->error->NULLポインター\n" ); 51 } 52} 53 54//bool outputLogFile() 55//{ 56// ofstream outputLogFile; 57// outputLogFile.open( "G:\\問題23.log", ios::app ); 58// outputLogFile << gLogstr << endl; 59// outputLogFile.close(); 60// return true; 61//} 62 63bool outputLogFile() 64{ 65 ofstream outputLog; 66 outputLog.open( "C:\\ログファイル.log", ios::app ); 67 68 /*confirmFileOpen( outputLogFile );*/ 69 70 SYSTEMTIME UTC; 71 SYSTEMTIME Local; 72 TIME_ZONE_INFORMATION getTimezone; 73 GetTimeZoneInformation( &getTimezone ); 74 GetSystemTime( &UTC ); 75 SystemTimeToTzSpecificLocalTime( &getTimezone, &UTC, &Local ); 76 77 outputLog << Local.wYear << "/" << Local.wYear << "/" << Local.wMonth << "/" 78 << Local.wDay << "/" << Local.wHour << ":" << Local.wMinute << ":" 79 << Local.wSecond << ":" << Local.wMilliseconds; 80 81 outputLog << gLogMessage << endl; 82 outputLog.close(); 83 return true; 84} 85 86void confirmFileIsOpened( ofstream outputLogFile ) 87{ 88 if( false == outputLogFile.is_open() ){ 89 cout << "ファイルが開けませんでした。" << endl; 90 writeLog( "->File couldn't open!\n" ); 91 } 92 writeLog( "->File was opened.\n" ); 93} 94 95void confirmOutputSuccessOrFail() 96{ 97 int nOutputLogFile = outputLogFile(); 98 if( true == nOutputLogFile ){ 99 cout << "ファイルを作成し、書き込みました。" << endl; 100 writeLog( "->LogFile was outputted.\n" ); 101 } 102 else{ 103 cout << "ファイル作成出来ませんでした。" << endl; 104 writeLog( "->LogFile couldn't output!\n" ); 105 } 106} 107 108int inputNum() 109{ 110 int nNum = 0; 111 while( 1 ){ 112 cout << "数値を入力してください。" << endl;; 113 cin >> nNum; 114 if( cin.fail() ){ 115 cin.clear(); 116 cin.ignore( 256, '\n' ); 117 writeLog( "->error input!\n" ); 118 continue; 119 } 120 break; 121 } 122 writeLog( "->Numbers were inputted.\n" ); 123 return nNum; 124} 125 126int main() 127{ 128 int nFirstNum = inputNum(); 129 int nLastNum = inputNum(); 130 131 swap( &nFirstNum, &nLastNum ); 132 133 cout << "\n処理結果 \n" << endl; 134 cout << " firstNumber = " << nFirstNum << endl; 135 cout << " secondNumber = " << nLastNum << endl; 136 137 outputLogFile(); 138 confirmOutputSuccessOrFail(); 139 140 return 0; 141} 142
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。