// OutputLogTest.cpp : コンソール アプリケーションのエントリ // ポイントを定義します。 // #include "stdafx.h" #include "windows.h" int OutputLog ( const char* format, ... ); int _tmain( int argc, _TCHAR* argv[] ) { OutputLog( "OutputLogTest START" ); Sleep( 1000 ); OutputLog( "OutputLogTest END" ); return 0; } //=============================================================== // [関数名] OutputLog // // [概 要] 実行ファイルを同じフォルダにログを出力する。 // // [引 数] const char* format ログ内容(255文字まで) // // [戻り値] 0:成功 // -1:ログファイルOpen失敗 //=============================================================== int OutputLog( const char* format, ... ) { FILE* fp = NULL; fopen_s( &fp, "OutputLog.log", "a" ); if ( fp == NULL ) { return -1; } // 現在のローカル時刻を取得 SYSTEMTIME st; GetLocalTime( &st ); char buf[255+1]; va_list ap; va_start( ap, format ); vsprintf_s( buf, sizeof( buf ), format, ap ); va_end( ap ); fprintf( fp , "%04d/%02d/%02d %02d:%02d:%02d.%03d %s\n" , st.wYear , st.wMonth , st.wDay , st.wHour , st.wMinute , st.wSecond , st.wMilliseconds , buf ); fclose( fp ); return 0; }