Browse Source

global cout and cerr is now retrieved using global::cout() and global::cerr()

This is necessary to avoid global variable initialization order
problem.
Tatsuhiro Tsujikawa 14 years ago
parent
commit
37016c6587
7 changed files with 64 additions and 64 deletions
  1. 9 9
      src/ConsoleStatCalc.cc
  2. 6 6
      src/Logger.cc
  3. 2 4
      src/Platform.cc
  4. 19 17
      src/console.cc
  5. 4 6
      src/console.h
  6. 10 8
      src/main.cc
  7. 14 14
      src/option_processing.cc

+ 9 - 9
src/ConsoleStatCalc.cc

@@ -196,7 +196,7 @@ public:
                   o, rg->inMemoryDownload());
     o << "\n"
       << std::setfill(SEP_CHAR) << std::setw(cols_) << SEP_CHAR << "\n";
-    global::cout->write(o.str().c_str());
+    global::cout()->write(o.str().c_str());
   }
 };
 } // namespace
@@ -227,7 +227,7 @@ void printProgressSummary
   }
   o << " *** " << "\n"
     << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
-  global::cout->write(o.str().c_str());
+  global::cout()->write(o.str().c_str());
   std::for_each(groups.begin(), groups.end(),
                 PrintSummary(cols, e, sizeFormatter));
 }
@@ -273,7 +273,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
 #endif // HAVE_TERMIOS_H
 #endif // !__MINGW32__
     std::string line(cols, ' ');
-    global::cout->printf("\r%s\r", line.c_str());
+    global::cout()->printf("\r%s\r", line.c_str());
   }
   std::ostringstream o;
   if(e->getRequestGroupMan()->countRequestGroup() > 0) {
@@ -283,8 +283,8 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
       lastSummaryNotified_ = global::wallclock();
       printProgressSummary(e->getRequestGroupMan()->getRequestGroups(), cols, e,
                            sizeFormatter);
-      global::cout->write("\n");
-      global::cout->flush();
+      global::cout()->write("\n");
+      global::cout()->flush();
     }
   }
   if(!readoutVisibility_) {
@@ -372,11 +372,11 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
     if(truncate_ && readout.size() > cols) {
       readout[cols] = '\0';
     }
-    global::cout->write(readout.c_str());
-    global::cout->flush();
+    global::cout()->write(readout.c_str());
+    global::cout()->flush();
   } else {
-    global::cout->write(readout.c_str());
-    global::cout->write("\n");
+    global::cout()->write(readout.c_str());
+    global::cout()->write("\n");
   }
 }
 

+ 6 - 6
src/Logger.cc

@@ -73,7 +73,7 @@ void Logger::openFile(const std::string& filename)
 {
   closeFile();
   if(filename == DEV_STDOUT) {
-    fpp_ = global::cout;
+    fpp_ = global::cout();
   } else {
     fpp_.reset(new BufferedFile(filename, BufferedFile::APPEND));
     if(!fpp_) {
@@ -170,11 +170,11 @@ void Logger::writeLog
     fpp_->flush();
   }
   if(toConsole) {
-    global::cout->printf("\n");
-    writeHeader(*global::cout, level, 0, 0);
-    global::cout->printf("%s\n", msg);
-    writeStackTrace(*global::cout, trace);
-    global::cout->flush();
+    global::cout()->printf("\n");
+    writeHeader(*global::cout(), level, 0, 0);
+    global::cout()->printf("%s\n", msg);
+    writeStackTrace(*global::cout(), trace);
+    global::cout()->flush();
   }
 }
 

+ 2 - 4
src/Platform.cc

@@ -95,8 +95,6 @@ bool Platform::setUp()
   }
   initialized_ = true;
 
-  global::initConsole();
-
 #ifdef ENABLE_NLS
   setlocale (LC_CTYPE, "");
   setlocale (LC_MESSAGES, "");
@@ -129,8 +127,8 @@ bool Platform::setUp()
 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
   int aresErrorCode;
   if((aresErrorCode = ares_library_init(ARES_LIB_INIT_ALL)) != 0) {
-    global::cerr->printf("ares_library_init() failed:%s\n",
-                         ares_strerror(aresErrorCode));
+    global::cerr()->printf("ares_library_init() failed:%s\n",
+                           ares_strerror(aresErrorCode));
   }
 #endif // CARES_HAVE_ARES_LIBRARY_INIT
 

+ 19 - 17
src/console.cc

@@ -39,30 +39,32 @@ namespace aria2 {
 namespace global {
 
 #ifdef __MINGW32__
-SharedHandle<WinConsoleFile> cout;
+const SharedHandle<WinConsoleFile>& cout()
+{
+  static SharedHandle<WinConsoleFile> f(new WinConsoleFile(STD_INPUT_HANDLE));
+  return f;
+}
 #else // !__MINGW32__
-SharedHandle<BufferedFile> cout;
+const SharedHandle<BufferedFile>& cout()
+{
+  static SharedHandle<BufferedFile> f(new BufferedFile(stdout));
+  return f;
+}
 #endif // !__MINGW32__
 
 #ifdef __MINGW32__
-SharedHandle<WinConsoleFile> cerr;
-#else // !__MINGW32__
-SharedHandle<BufferedFile> cerr;
-#endif // !__MINGW32__
-
-void initConsole()
+const SharedHandle<WinConsoleFile>& cerr()
 {
-#ifdef __MINGW32__
-  cout.reset(new WinConsoleFile(STD_INPUT_HANDLE));
-#else // !__MINGW32__
-  cout.reset(new BufferedFile(stdout));
-#endif // !__MINGW32__
-#ifdef __MINGW32__
-  cerr.reset(new WinConsoleFile(STD_ERROR_HANDLE));
+  static SharedHandle<WinConsoleFile> f(new WinConsoleFile(STD_ERROR_HANDLE));
+  return f;
+}
 #else // !__MINGW32__
-  cerr.reset(new BufferedFile(stderr));
-#endif // !__MINGW32__
+const SharedHandle<BufferedFile>& cerr()
+{
+  static SharedHandle<BufferedFile> f(new BufferedFile(stderr));
+  return f;
 }
+#endif // !__MINGW32__
 
 } // namespace global
 

+ 4 - 6
src/console.h

@@ -48,19 +48,17 @@ namespace aria2 {
 namespace global {
 
 #ifdef __MINGW32__
-extern SharedHandle<WinConsoleFile> cout;
+const SharedHandle<WinConsoleFile>& cout();
 #else // !__MINGW32__
-extern SharedHandle<BufferedFile> cout;
+const SharedHandle<BufferedFile>& cout();
 #endif // !__MINGW32__
 
 #ifdef __MINGW32__
-extern SharedHandle<WinConsoleFile> cerr;
+const SharedHandle<WinConsoleFile>& cerr();
 #else // !__MINGW32__
-extern SharedHandle<BufferedFile> cerr;
+const SharedHandle<BufferedFile>& cerr();
 #endif // !__MINGW32__
 
-void initConsole();
-
 } // namespace global
 
 } // namespace aria2

+ 10 - 8
src/main.cc

@@ -107,7 +107,7 @@ SharedHandle<OutputFile> getSummaryOut(const SharedHandle<Option>& op)
   if(op->getAsBool(PREF_QUIET)) {
     return SharedHandle<OutputFile>(new NullOutputFile());
   } else {
-    return global::cout;
+    return global::cout();
   }
 }
 
@@ -118,7 +118,7 @@ void showTorrentFile(const std::string& uri)
   SharedHandle<Option> op(new Option());
   SharedHandle<DownloadContext> dctx(new DownloadContext());
   bittorrent::load(uri, dctx, op);
-  bittorrent::print(*global::cout, dctx);
+  bittorrent::print(*global::cout(), dctx);
 }
 } // namespace
 #endif // ENABLE_BITTORRENT
@@ -133,9 +133,9 @@ void showMetalinkFile
                           op->get(PREF_METALINK_BASE_URI));
   std::vector<SharedHandle<FileEntry> > fileEntries;
   MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
-  util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout);
-  global::cout->write("\n");
-  global::cout->flush();
+  util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout());
+  global::cout()->write("\n");
+  global::cout()->flush();
 }
 } // namespace
 #endif // ENABLE_METALINK
@@ -167,7 +167,7 @@ void showFiles
             printf("\n\n");
           }
     } catch(RecoverableException& e) {
-      global::cout->printf("%s\n", e.stackTrace().c_str());
+      global::cout()->printf("%s\n", e.stackTrace().c_str());
     }
   }
 }
@@ -268,7 +268,7 @@ error_code::Value main(int argc, char* argv[])
   op->remove(PREF_SELECT_FILE);
   op->remove(PREF_PAUSE);
   if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty()) {
-    global::cout->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
+    global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
   } else {
     exitStatus = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
                                      getSummaryOut(op)).execute();
@@ -285,7 +285,9 @@ int main(int argc, char* argv[])
     aria2::Platform platform;
     r = aria2::main(argc, argv);
   } catch(aria2::Exception& ex) {
-    std::cerr << EX_EXCEPTION_CAUGHT << "\n" << ex.stackTrace() << std::endl;
+    aria2::global::cerr()->printf("%s\n%s\n",
+                                  EX_EXCEPTION_CAUGHT,
+                                  ex.stackTrace().c_str());
     r = ex.getErrorCode();
   }
   return r;

+ 14 - 14
src/option_processing.cc

@@ -76,7 +76,7 @@ void overrideWithEnv(Option& op, const OptionParser& optionParser,
     try {
       optionParser.findByName(pref)->parse(op, value);
     } catch(Exception& e) {
-      global::cerr->printf
+      global::cerr()->printf
         ("Caught Error while parsing environment variable '%s'\n%s\n",
          envName.c_str(),
          e.stackTrace().c_str());
@@ -144,25 +144,25 @@ void option_processing(Option& op, std::vector<std::string>& uris,
         try {
           oparser.parse(op, ss);
         } catch(OptionHandlerException& e) {
-          global::cerr->printf("Parse error in %s\n%s\n",
-                               cfname.c_str(),
-                               e.stackTrace().c_str());
+          global::cerr()->printf("Parse error in %s\n%s\n",
+                                 cfname.c_str(),
+                                 e.stackTrace().c_str());
           SharedHandle<OptionHandler> h = oparser.findByName(e.getOptionName());
           if(h) {
-            global::cerr->printf
+            global::cerr()->printf
               ("Usage:\n%s\n",
                oparser.findByName(e.getOptionName())->getDescription().c_str());
           }
           exit(e.getErrorCode());
         } catch(Exception& e) {
-          global::cerr->printf("Parse error in %s\n%s\n",
-                               cfname.c_str(),
-                               e.stackTrace().c_str());
+          global::cerr()->printf("Parse error in %s\n%s\n",
+                                 cfname.c_str(),
+                                 e.stackTrace().c_str());
           exit(e.getErrorCode());
         }
       } else if(!ucfname.empty()) {
-        global::cerr->printf("Configuration file %s is not found.\n",
-                             cfname.c_str());
+        global::cerr()->printf("Configuration file %s is not found.\n",
+                               cfname.c_str());
         showUsage(TAG_HELP, oparser);
         exit(error_code::UNKNOWN_ERROR);
       }
@@ -188,16 +188,16 @@ void option_processing(Option& op, std::vector<std::string>& uris,
     }
 #endif // __MINGW32__
   } catch(OptionHandlerException& e) {
-    global::cerr->printf("%s\n", e.stackTrace().c_str());
+    global::cerr()->printf("%s\n", e.stackTrace().c_str());
     SharedHandle<OptionHandler> h = oparser.findByName(e.getOptionName());
     if(h) {
       std::ostringstream ss;
       ss << *h;
-      global::cerr->printf("Usage:\n%s\n", ss.str().c_str());
+      global::cerr()->printf("Usage:\n%s\n", ss.str().c_str());
     }
     exit(e.getErrorCode());
   } catch(Exception& e) {
-    global::cerr->printf("%s\n", e.stackTrace().c_str());
+    global::cerr()->printf("%s\n", e.stackTrace().c_str());
     showUsage(TAG_HELP, oparser);
     exit(e.getErrorCode());
   }
@@ -210,7 +210,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
 #endif // ENABLE_METALINK
      op.blank(PREF_INPUT_FILE)) {
     if(uris.empty()) {
-      global::cerr->printf("%s\n", MSG_URI_REQUIRED);
+      global::cerr()->printf("%s\n", MSG_URI_REQUIRED);
       showUsage(TAG_HELP, oparser);
       exit(error_code::UNKNOWN_ERROR);
     }