Browse Source

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 năm trước cách đây
mục cha
commit
97f34ab668

+ 0 - 4
src/AbstractDiskWriter.cc

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

+ 0 - 5
src/File.cc

@@ -52,11 +52,6 @@
 
 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 File& c) : name_(c.name_) {}

+ 2 - 3
src/Platform.cc

@@ -129,9 +129,8 @@ bool Platform::setUp()
 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
   int aresErrorCode;
   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
 

+ 1 - 1
src/ServerStat.cc

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

+ 2 - 2
src/ServerStat.h

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

+ 11 - 14
src/WinConsoleFile.cc

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

+ 2 - 1
src/WinConsoleFile.h

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

+ 8 - 0
src/common.h

@@ -59,6 +59,14 @@ typedef _off_t off_t;
 #endif
 #endif // __MINGW32__
 
+#ifdef __MINGW32__
+# define WIN32_LEAN_AND_MEAN
+# ifndef WINVER
+#  define WINVER 0x501u
+# endif // WINVER
+# include <windows.h>
+#endif // __MINGW32__
+
 #ifdef ENABLE_NLS
 // If we put #include <gettext.h> outside of #ifdef ENABLE_NLS and
 // --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;
 #endif // !__MINGW32__
 
+#ifdef __MINGW32__
+SharedHandle<WinConsoleFile> cerr;
+#else // !__MINGW32__
+SharedHandle<BufferedFile> cerr;
+#endif // !__MINGW32__
+
 void initConsole()
 {
 #ifdef __MINGW32__
-  cout.reset(new WinConsoleFile());
+  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));
+#else // !__MINGW32__
+  cerr.reset(new BufferedFile(stderr));
+#endif // !__MINGW32__
 }
 
 } // namespace global

+ 6 - 0
src/console.h

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

+ 22 - 19
src/option_processing.cc

@@ -56,6 +56,7 @@
 #include "SimpleRandomizer.h"
 #include "bittorrent_helper.h"
 #include "BufferedFile.h"
+#include "console.h"
 #ifndef HAVE_DAEMON
 #include "daemon.h"
 #endif // !HAVE_DAEMON
@@ -75,10 +76,10 @@ void overrideWithEnv(Option& op, const OptionParser& optionParser,
     try {
       optionParser.findByName(pref)->parse(op, value);
     } 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 {
           oparser.parse(op, ss);
         } 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());
           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());
         } 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());
         }
       } 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);
         exit(error_code::UNKNOWN_ERROR);
       }
@@ -185,16 +188,16 @@ void option_processing(Option& op, std::vector<std::string>& uris,
     }
 #endif // __MINGW32__
   } catch(OptionHandlerException& e) {
-    std::cerr << e.stackTrace() << "\n";
+    global::cerr->printf("%s\n", e.stackTrace().c_str());
     SharedHandle<OptionHandler> h = oparser.findByName(e.getOptionName());
     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());
   } catch(Exception& e) {
-    std::cerr << e.stackTrace() << std::endl;
+    global::cerr->printf("%s\n", e.stackTrace().c_str());
     showUsage(TAG_HELP, oparser);
     exit(e.getErrorCode());
   }
@@ -207,7 +210,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
 #endif // ENABLE_METALINK
      op.blank(PREF_INPUT_FILE)) {
     if(uris.empty()) {
-      std::cerr << MSG_URI_REQUIRED << std::endl;
+      global::cerr->printf("%s\n", MSG_URI_REQUIRED);
       showUsage(TAG_HELP, oparser);
       exit(error_code::UNKNOWN_ERROR);
     }

+ 0 - 6
src/util.cc

@@ -48,12 +48,6 @@
 #include <algorithm>
 #include <fstream>
 #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
 # include <gcrypt.h>

+ 3 - 3
test/ServerStatManTest.cc

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

+ 2 - 2
test/ServerStatTest.cc

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