1717
1818using namespace std ;
1919
20+
21+ FILE* s_file_logger = NULL ;
22+ #define my_print (...) {printf (__VA_ARGS__);if (s_file_logger){fprintf (s_file_logger, __VA_ARGS__);}; }
23+
24+
2025static void sys_sleep (uint32_t nMilliseconds)
2126{
2227 std::this_thread::sleep_for (std::chrono::milliseconds (nMilliseconds));
@@ -156,7 +161,7 @@ class win10_wubi_patch
156161 } else {
157162 s_bok = " failed" ;
158163 }
159- printf (" -- do patch, pid = %d, times = %d, result = %s\n " , pid, count, s_bok.c_str ());
164+ my_print (" -- do patch, pid = %d, times = %d, result = %s\n " , pid, count, s_bok.c_str ());
160165 }
161166 }
162167 }
@@ -232,24 +237,75 @@ bool check_support_version()
232237
233238static void Usage ()
234239{
235- printf (" if you want to exit when patch success, pass '--exit_when_patched' to launch command\n " );
240+ my_print (" if you want to exit when patch success, pass '--exit_when_patched' to launch command\n " );
236241}
237242
243+ class file_keeper
244+ {
245+ public:
246+ file_keeper ()
247+ {
248+ _file = NULL ;
249+ }
250+ ~file_keeper ()
251+ {
252+ Close ();
253+ }
254+ public:
255+ bool Open (const std::string& filename)
256+ {
257+ Close ();
258+ bool bOK = false ;
259+ do
260+ {
261+ FILE* file = fopen (filename.c_str (), " wb" );
262+ if (!file) {
263+ break ;
264+ }
265+
266+ _file = file;
267+ bOK = true ;
268+ } while (0 );
269+ return bOK;
270+ }
271+
272+ FILE* GetFile ()
273+ {
274+ return _file;
275+ }
276+
277+ void Close ()
278+ {
279+ if (_file) {
280+ fclose (_file);
281+ _file = NULL ;
282+ }
283+ }
284+ private:
285+ FILE* _file;
286+ };
287+
238288int main (int argc, char * argv[])
239289{
240- printf (" ---Widows 10 Disable English Switch Key(Shift) For Wubi InputMethod---\n " );
241- printf (" key press [Q] to exit\n " );
290+ file_keeper logger;
291+ std::string app_data = getenv (" appdata" );
292+ app_data += " \\ wubi_patch.log" ;
293+ logger.Open (app_data);
294+ s_file_logger = logger.GetFile ();
295+
296+ my_print (" ---Widows 10 Disable English Switch Key(Shift) For Wubi InputMethod---\n " );
297+ my_print (" key press [Q] to exit\n " );
242298 Usage ();
243- printf (" \n " );
299+ my_print (" \n " );
244300
245301 if (!check_support_version ()) {
246- printf (" file(ChsIME.exe) not support, need send ChsIME.exe to author.\n " );
302+ my_print (" file(ChsIME.exe) not support, need send ChsIME.exe to author.\n " );
247303 getchar ();
248304 return -1 ;
249305 }
250306
251307 if (!set_debug_privilege ()) {
252- printf (" admin privileges are required.\n " );
308+ my_print (" admin privileges are required.\n " );
253309 return -1 ;
254310 }
255311
@@ -267,14 +323,14 @@ int main(int argc, char* argv[])
267323 wubi_patch.RunThread ();
268324 do {
269325 if (read_key () == ' q' ) {
270- printf (" exit(user)\n " );
326+ my_print (" exit(user)\n " );
271327 sys_sleep (500 );
272328 break ;
273329 }
274330
275331 if (exit_when_patched) {
276332 if (wubi_patch.CheckExit ()) {
277- printf (" exit(patched)\n " );
333+ my_print (" exit(patched)\n " );
278334 sys_sleep (500 );
279335 break ;
280336 }
0 commit comments