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.
pull/1/head
Tatsuhiro Tsujikawa 2011-08-10 00:38:48 +09:00
parent a10cda2f17
commit 97f34ab668
14 changed files with 71 additions and 61 deletions

View File

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

View File

@ -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_) {}

View File

@ -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

View File

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

View File

@ -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_

View File

@ -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;

View File

@ -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&);

View File

@ -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 *)

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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>

View File

@ -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;

View File

@ -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.