瀏覽代碼

Added global::cerr. windows.h now included from common.h

We replaced most of std::cerr with global::cerr.  windows.h is now
included from common.h. Before including it, we define WINVER.  We
renamed some variable name because some macros in windows.h collide
with them.
Tatsuhiro Tsujikawa 14 年之前
父節點
當前提交
97f34ab668
共有 14 個文件被更改,包括 71 次插入61 次删除
  1. 0 4
      src/AbstractDiskWriter.cc
  2. 0 5
      src/File.cc
  3. 2 3
      src/Platform.cc
  4. 1 1
      src/ServerStat.cc
  5. 2 2
      src/ServerStat.h
  6. 11 14
      src/WinConsoleFile.cc
  7. 2 1
      src/WinConsoleFile.h
  8. 8 0
      src/common.h
  9. 12 1
      src/console.cc
  10. 6 0
      src/console.h
  11. 22 19
      src/option_processing.cc
  12. 0 6
      src/util.cc
  13. 3 3
      test/ServerStatManTest.cc
  14. 2 2
      test/ServerStatTest.cc

+ 0 - 4
src/AbstractDiskWriter.cc

@@ -40,10 +40,6 @@
 #include <cstring>
 #include <cstring>
 #include <cassert>
 #include <cassert>
 
 
-#ifdef __MINGW32__
-# include <windows.h>
-#endif // __MINGW32__
-
 #include "File.h"
 #include "File.h"
 #include "util.h"
 #include "util.h"
 #include "message.h"
 #include "message.h"

+ 0 - 5
src/File.cc

@@ -52,11 +52,6 @@
 
 
 namespace aria2 {
 namespace aria2 {
 
 
-#ifdef __MINGW32__
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-#endif // __MINGW32__
-
 File::File(const std::string& name) : name_(name) {}
 File::File(const std::string& name) : name_(name) {}
 
 
 File::File(const File& c) : name_(c.name_) {}
 File::File(const File& c) : name_(c.name_) {}

+ 2 - 3
src/Platform.cc

@@ -129,9 +129,8 @@ bool Platform::setUp()
 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
   int aresErrorCode;
   int aresErrorCode;
   if((aresErrorCode = ares_library_init(ARES_LIB_INIT_ALL)) != 0) {
   if((aresErrorCode = ares_library_init(ARES_LIB_INIT_ALL)) != 0) {
-    std::cerr << "ares_library_init() failed:"
-              << ares_strerror(aresErrorCode)
-              << std::endl;
+    global::cerr->printf("ares_library_init() failed:%s\n",
+                         ares_strerror(aresErrorCode));
   }
   }
 #endif // CARES_HAVE_ARES_LIBRARY_INIT
 #endif // CARES_HAVE_ARES_LIBRARY_INIT
 
 

+ 1 - 1
src/ServerStat.cc

@@ -188,7 +188,7 @@ void ServerStat::setOK()
 
 
 void ServerStat::setError()
 void ServerStat::setError()
 {
 {
-  setStatusInternal(ERROR);
+  setStatusInternal(A2_ERROR);
 }
 }
 
 
 bool ServerStat::operator<(const ServerStat& serverStat) const
 bool ServerStat::operator<(const ServerStat& serverStat) const

+ 2 - 2
src/ServerStat.h

@@ -52,7 +52,7 @@ class ServerStat {
 public:
 public:
   enum STATUS {
   enum STATUS {
     OK = 0,
     OK = 0,
-    ERROR
+    A2_ERROR
   };
   };
   
   
   static const std::string STATUS_STRING[];
   static const std::string STATUS_STRING[];
@@ -137,7 +137,7 @@ public:
 
 
   bool isError() const
   bool isError() const
   {
   {
-    return status_ == ERROR;
+    return status_ == A2_ERROR;
   }
   }
 
 
   // set status ERROR and update lastUpdated_
   // set status ERROR and update lastUpdated_

+ 11 - 14
src/WinConsoleFile.cc

@@ -39,37 +39,34 @@
 #include <cstdarg>
 #include <cstdarg>
 #include <string>
 #include <string>
 
 
-#ifdef __MINGW32__
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-#endif // __MINGW32__
-
 #include "a2io.h"
 #include "a2io.h"
 #include "util.h"
 #include "util.h"
 
 
 namespace aria2 {
 namespace aria2 {
 
 
-WinConsoleFile::WinConsoleFile() {}
+WinConsoleFile::WinConsoleFile(DWORD stdHandle)
+  : stdHandle_(stdHandle)
+{}
 
 
 WinConsoleFile::~WinConsoleFile() {}
 WinConsoleFile::~WinConsoleFile() {}
 
 
 namespace {
 namespace {
-bool console()
+bool console(DWORD stdHandle)
 {
 {
   DWORD mode;
   DWORD mode;
-  return GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode);
+  return GetConsoleMode(GetStdHandle(stdHandle), &mode);
 }
 }
 } // namespace
 } // namespace
 
 
 size_t WinConsoleFile::write(const char* str)
 size_t WinConsoleFile::write(const char* str)
 {
 {
   DWORD written;
   DWORD written;
-  if(console()) {
+  if(console(stdHandle_)) {
     std::wstring msg = utf8ToWChar(str);
     std::wstring msg = utf8ToWChar(str);
-    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
+    WriteConsoleW(GetStdHandle(stdHandle_),
                   msg.c_str(), msg.size(), &written, 0);
                   msg.c_str(), msg.size(), &written, 0);
   } else {
   } else {
-    WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
+    WriteFile(GetStdHandle(stdHandle_),
               str, strlen(str), &written, 0);
               str, strlen(str), &written, 0);
   }
   }
   return written;
   return written;
@@ -86,12 +83,12 @@ int WinConsoleFile::printf(const char* format, ...)
     return 0;
     return 0;
   }
   }
   DWORD written;
   DWORD written;
-  if(console()) {
+  if(console(stdHandle_)) {
     std::wstring msg = utf8ToWChar(buf);
     std::wstring msg = utf8ToWChar(buf);
-    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
+    WriteConsoleW(GetStdHandle(stdHandle_),
                   msg.c_str(), msg.size(), &written, 0);
                   msg.c_str(), msg.size(), &written, 0);
   } else {
   } else {
-    WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
+    WriteFile(GetStdHandle(stdHandle_),
               buf, r, &written, 0);
               buf, r, &written, 0);
   }
   }
   return written;
   return written;

+ 2 - 1
src/WinConsoleFile.h

@@ -42,12 +42,13 @@ namespace aria2 {
 // This is a wrapper class for WriteConsoleW
 // This is a wrapper class for WriteConsoleW
 class WinConsoleFile:public OutputFile {
 class WinConsoleFile:public OutputFile {
 public:
 public:
-  WinConsoleFile();
+  WinConsoleFile(DWORD stdHandle);
   virtual ~WinConsoleFile();
   virtual ~WinConsoleFile();
   virtual size_t write(const char* str);
   virtual size_t write(const char* str);
   virtual int printf(const char* format, ...);
   virtual int printf(const char* format, ...);
   virtual int flush();
   virtual int flush();
 private:
 private:
+  DWORD stdHandle_;
   // Don't allow copying
   // Don't allow copying
   WinConsoleFile(const WinConsoleFile&);
   WinConsoleFile(const WinConsoleFile&);
   WinConsoleFile& operator=(const WinConsoleFile&);
   WinConsoleFile& operator=(const WinConsoleFile&);

+ 8 - 0
src/common.h

@@ -59,6 +59,14 @@ typedef _off_t off_t;
 #endif
 #endif
 #endif // __MINGW32__
 #endif // __MINGW32__
 
 
+#ifdef __MINGW32__
+# define WIN32_LEAN_AND_MEAN
+# ifndef WINVER
+#  define WINVER 0x501u
+# endif // WINVER
+# include <windows.h>
+#endif // __MINGW32__
+
 #ifdef ENABLE_NLS
 #ifdef ENABLE_NLS
 // If we put #include <gettext.h> outside of #ifdef ENABLE_NLS and
 // If we put #include <gettext.h> outside of #ifdef ENABLE_NLS and
 // --disable-nls is used, gettext(msgid) is defined as ((const char *)
 // --disable-nls is used, gettext(msgid) is defined as ((const char *)

+ 12 - 1
src/console.cc

@@ -44,13 +44,24 @@ SharedHandle<WinConsoleFile> cout;
 SharedHandle<BufferedFile> cout;
 SharedHandle<BufferedFile> cout;
 #endif // !__MINGW32__
 #endif // !__MINGW32__
 
 
+#ifdef __MINGW32__
+SharedHandle<WinConsoleFile> cerr;
+#else // !__MINGW32__
+SharedHandle<BufferedFile> cerr;
+#endif // !__MINGW32__
+
 void initConsole()
 void initConsole()
 {
 {
 #ifdef __MINGW32__
 #ifdef __MINGW32__
-  cout.reset(new WinConsoleFile());
+  cout.reset(new WinConsoleFile(STD_INPUT_HANDLE));
 #else // !__MINGW32__
 #else // !__MINGW32__
   cout.reset(new BufferedFile(stdout));
   cout.reset(new BufferedFile(stdout));
 #endif // !__MINGW32__
 #endif // !__MINGW32__
+#ifdef __MINGW32__
+  cerr.reset(new WinConsoleFile(STD_ERROR_HANDLE));
+#else // !__MINGW32__
+  cerr.reset(new BufferedFile(stderr));
+#endif // !__MINGW32__
 }
 }
 
 
 } // namespace global
 } // namespace global

+ 6 - 0
src/console.h

@@ -53,6 +53,12 @@ extern SharedHandle<WinConsoleFile> cout;
 extern SharedHandle<BufferedFile> cout;
 extern SharedHandle<BufferedFile> cout;
 #endif // !__MINGW32__
 #endif // !__MINGW32__
 
 
+#ifdef __MINGW32__
+extern SharedHandle<WinConsoleFile> cerr;
+#else // !__MINGW32__
+extern SharedHandle<BufferedFile> cerr;
+#endif // !__MINGW32__
+
 void initConsole();
 void initConsole();
 
 
 } // namespace global
 } // namespace global

+ 22 - 19
src/option_processing.cc

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

+ 0 - 6
src/util.cc

@@ -48,12 +48,6 @@
 #include <algorithm>
 #include <algorithm>
 #include <fstream>
 #include <fstream>
 #include <iomanip>
 #include <iomanip>
-#ifndef HAVE_SLEEP
-# ifdef HAVE_WINSOCK_H
-#  define WIN32_LEAN_AND_MEAN
-#  include <windows.h>
-# endif // HAVE_WINSOCK_H
-#endif // HAVE_SLEEP
 
 
 #ifdef HAVE_LIBGCRYPT
 #ifdef HAVE_LIBGCRYPT
 # include <gcrypt.h>
 # include <gcrypt.h>

+ 3 - 3
test/ServerStatManTest.cc

@@ -71,7 +71,7 @@ void ServerStatManTest::testSave()
   localhost_ftp->setLastUpdated(Time(1210000001));
   localhost_ftp->setLastUpdated(Time(1210000001));
   SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
   SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
   mirror->setDownloadSpeed(0);
   mirror->setDownloadSpeed(0);
-  mirror->setStatus(ServerStat::ERROR);
+  mirror->setStatus(ServerStat::A2_ERROR);
   mirror->setLastUpdated(Time(1210000002));
   mirror->setLastUpdated(Time(1210000002));
 
 
   ServerStatMan ssm;
   ServerStatMan ssm;
@@ -141,7 +141,7 @@ void ServerStatManTest::testLoad()
 
 
   SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
   SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
   CPPUNIT_ASSERT(mirror);
   CPPUNIT_ASSERT(mirror);
-  CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
+  CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, mirror->getStatus());
 }
 }
 
 
 void ServerStatManTest::testRemoveStaleServerStat()
 void ServerStatManTest::testRemoveStaleServerStat()
@@ -155,7 +155,7 @@ void ServerStatManTest::testRemoveStaleServerStat()
   localhost_ftp->setLastUpdated(Time(1210000001));
   localhost_ftp->setLastUpdated(Time(1210000001));
   SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
   SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
   mirror->setDownloadSpeed(0);
   mirror->setDownloadSpeed(0);
-  mirror->setStatus(ServerStat::ERROR);
+  mirror->setStatus(ServerStat::A2_ERROR);
   mirror->setLastUpdated(Time(1210000002));
   mirror->setLastUpdated(Time(1210000002));
 
 
   ServerStatMan ssm;
   ServerStatMan ssm;

+ 2 - 2
test/ServerStatTest.cc

@@ -33,10 +33,10 @@ void ServerStatTest::testSetStatus()
   ServerStat ss("localhost", "http");
   ServerStat ss("localhost", "http");
   CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
   CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
   ss.setStatus("ERROR");
   ss.setStatus("ERROR");
-  CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, ss.getStatus());
+  CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, ss.getStatus());
   // See undefined status string will not change current status.
   // See undefined status string will not change current status.
   ss.setStatus("__BADSTATUS");
   ss.setStatus("__BADSTATUS");
-  CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, ss.getStatus());
+  CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, ss.getStatus());
   ss.setStatus("OK");
   ss.setStatus("OK");
   CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
   CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
   // See undefined status string will not change current status.
   // See undefined status string will not change current status.