summaryrefslogtreecommitdiffstats
path: root/src/Core
diff options
context:
space:
mode:
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/Application.cpp18
-rw-r--r--src/Core/Application.h18
-rw-r--r--src/Core/ConfigManager.cpp6
-rw-r--r--src/Core/Exception.cpp38
-rw-r--r--src/Core/Exception.h13
-rw-r--r--src/Core/LogManager.cpp16
-rw-r--r--src/Core/LogManager.h10
-rw-r--r--src/Core/Logger.h11
-rw-r--r--src/Core/String.cpp9
-rw-r--r--src/Core/String.h62
10 files changed, 120 insertions, 81 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp
index 666f2cb..86b5b7b 100644
--- a/src/Core/Application.cpp
+++ b/src/Core/Application.cpp
@@ -33,7 +33,7 @@ namespace Mad {
namespace Core {
Application::Application() {
- std::setlocale(LC_ALL, "");
+ std::setlocale(LC_ALL, "en_US.UTF-8");
configManager = new ConfigManager(this);
logManager = new LogManager(this);
@@ -47,7 +47,7 @@ Application::~Application() {
}
-void Application::logfv(Logger::MessageCategory category, Logger::MessageLevel level, const char *format, va_list ap) {
+void Application::logfv(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, va_list ap) {
int size = 100;
char *buf = (char*)std::malloc(size);
@@ -56,11 +56,11 @@ void Application::logfv(Logger::MessageCategory category, Logger::MessageLevel l
va_list ap2;
va_copy(ap2, ap);
- int n = vsnprintf(buf, size, format, ap2);
+ int n = vsnprintf(buf, size, format.toLocale().c_str(), ap2);
va_end(ap2);
if(n > -1 && n < size) {
- logManager->log(category, level, boost::posix_time::microsec_clock::universal_time(), buf);
+ logManager->log(category, level, boost::posix_time::microsec_clock::universal_time(), Core::String::fromLocale(buf));
std::free(buf);
return;
}
@@ -75,32 +75,32 @@ void Application::logfv(Logger::MessageCategory category, Logger::MessageLevel l
}
-void Application::log(Logger::MessageCategory category, Logger::MessageLevel level, const std::string &message) {
+void Application::log(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message) {
logManager->log(category, level, boost::posix_time::microsec_clock::universal_time(), message);
}
-void Application::logf(Logger::MessageCategory category, Logger::MessageLevel level, const char *format, ...) {
+void Application::logf(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, ...) {
va_list ap;
va_start(ap, format);
logfv(category, level, format, ap);
va_end(ap);
}
-void Application::logf(Logger::MessageCategory category, const char *format, ...) {
+void Application::logf(Logger::MessageCategory category, const Core::String &format, ...) {
va_list ap;
va_start(ap, format);
logfv(category, Logger::LOG_DEFAULT, format, ap);
va_end(ap);
}
-void Application::logf(Logger::MessageLevel level, const char *format, ...) {
+void Application::logf(Logger::MessageLevel level, const Core::String &format, ...) {
va_list ap;
va_start(ap, format);
logfv(Logger::LOG_GENERAL, level, format, ap);
va_end(ap);
}
-void Application::logf(const char *format, ...) {
+void Application::logf(const Core::String &format, ...) {
va_list ap;
va_start(ap, format);
logfv(Logger::LOG_GENERAL, Logger::LOG_DEFAULT, format, ap);
diff --git a/src/Core/Application.h b/src/Core/Application.h
index 18521d5..ffab639 100644
--- a/src/Core/Application.h
+++ b/src/Core/Application.h
@@ -45,7 +45,7 @@ class MAD_CORE_EXPORT Application : private boost::noncopyable {
LogManager *logManager;
ThreadManager *threadManager;
- void logfv(Logger::MessageCategory category, Logger::MessageLevel level, const char *format, va_list ap);
+ void logfv(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, va_list ap);
protected:
Application();
@@ -69,25 +69,25 @@ class MAD_CORE_EXPORT Application : private boost::noncopyable {
}
- void log(Logger::MessageCategory category, Logger::MessageLevel level, const std::string &message);
+ void log(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message);
- void log(Logger::MessageCategory category, const std::string &message) {
+ void log(Logger::MessageCategory category, const Core::String &message) {
log(category, Logger::LOG_DEFAULT, message);
}
- void log(Logger::MessageLevel level, const std::string &message) {
+ void log(Logger::MessageLevel level, const Core::String &message) {
log(Logger::LOG_GENERAL, level, message);
}
- void log(const std::string &message) {
+ void log(const Core::String &message) {
log(Logger::LOG_GENERAL, Logger::LOG_DEFAULT, message);
}
- void logf(Logger::MessageCategory category, Logger::MessageLevel level, const char *format, ...);
- void logf(Logger::MessageCategory category, const char *format, ...);
- void logf(Logger::MessageLevel level, const char *format, ...);
- void logf(const char *format, ...);
+ void logf(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, ...);
+ void logf(Logger::MessageCategory category, const Core::String &format, ...);
+ void logf(Logger::MessageLevel level, const Core::String &format, ...);
+ void logf(const Core::String &format, ...);
};
}
diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp
index d79ce32..fdbc93f 100644
--- a/src/Core/ConfigManager.cpp
+++ b/src/Core/ConfigManager.cpp
@@ -48,7 +48,7 @@ void ConfigManager::handleConfigEntry(const ConfigEntry &entry) {
}
if(!handled)
- application->logf(Logger::LOG_WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().extract().c_str());
+ application->logf(Logger::LOG_WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().toLocale().c_str());
}
bool ConfigManager::loadFile(const std::string &filename) {
@@ -66,9 +66,7 @@ bool ConfigManager::loadFile(const std::string &filename) {
while(!(file.eof() && line.isEmpty() && input.isEmpty())) {
while(input.isEmpty() && !file.eof()) {
- std::string tmp;
- std::getline(file, tmp);
- input = tmp.c_str();
+ input = String::getline(file);
}
if(input.isEmpty())
diff --git a/src/Core/Exception.cpp b/src/Core/Exception.cpp
index 220e6fd..338bd7c 100644
--- a/src/Core/Exception.cpp
+++ b/src/Core/Exception.cpp
@@ -25,60 +25,62 @@ namespace Mad {
namespace Core {
void Exception::updateWhatStr() {
- if(where.empty())
- whatStr.clear();
+ if(where.isEmpty())
+ errorStr.remove();
else
- whatStr = where + ": ";
+ errorStr = where + ": ";
switch(errorCode) {
case SUCCESS:
- whatStr += "Success";
+ errorStr += "Success";
break;
case UNEXPECTED_PACKET:
- whatStr += "An unexpected packet was received";
+ errorStr += "An unexpected packet was received";
break;
case INVALID_ACTION:
- whatStr += "The action is invalid";
+ errorStr += "The action is invalid";
break;
case NOT_AVAILABLE:
- whatStr += "Not available";
+ errorStr += "Not available";
break;
case NOT_FINISHED:
- whatStr += "Not finished";
+ errorStr += "Not finished";
break;
case NOT_IMPLEMENTED:
- whatStr += "Not implemented";
+ errorStr += "Not implemented";
break;
case NOT_FOUND:
- whatStr += "Not found";
+ errorStr += "Not found";
break;
case INVALID_INPUT:
- whatStr += "Invalid input";
+ errorStr += "Invalid input";
break;
case PERMISSION:
- whatStr += "Permission denied";
+ errorStr += "Permission denied";
break;
case INTERNAL_ERRNO:
- whatStr += std::strerror(subCode);
+ errorStr += std::strerror(subCode);
break;
case INVALID_ADDRESS:
- whatStr += "Invalid address";
+ errorStr += "Invalid address";
break;
case ALREADY_IDENTIFIED:
- whatStr += "The host is already identified";
+ errorStr += "The host is already identified";
break;
case UNKNOWN_DAEMON:
whatStr += "The daemon is unknown";
break;
case DUPLICATE_ENTRY:
- whatStr += "Duplicate entry";
+ errorStr += "Duplicate entry";
break;
case AUTHENTICATION:
- whatStr += "Authentication failure";
+ errorStr += "Authentication failure";
break;
default:
- whatStr += "Unknown error";
+ errorStr += "Unknown error";
}
+
+ whatStr = errorStr.toLocale();
}
}
diff --git a/src/Core/Exception.h b/src/Core/Exception.h
index 8e0342d..48688a4 100644
--- a/src/Core/Exception.h
+++ b/src/Core/Exception.h
@@ -43,18 +43,19 @@ class MAD_CORE_EXPORT Exception : public std::exception {
};
private:
- std::string where;
+ Core::String where;
ErrorCode errorCode;
long subCode;
long subSubCode;
+ Core::String errorStr;
std::string whatStr;
void updateWhatStr();
public:
- Exception(const std::string &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0)
+ Exception(const Core::String &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0)
: where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {
updateWhatStr();
}
@@ -63,7 +64,7 @@ class MAD_CORE_EXPORT Exception : public std::exception {
}
virtual ~Exception() throw () {}
- const std::string& getWhere() const {return where;}
+ const Core::String& getWhere() const {return where;}
ErrorCode getErrorCode() const {return errorCode;}
long getSubCode() const {return subCode;}
long getSubSubCode() const {return subSubCode;}
@@ -72,7 +73,11 @@ class MAD_CORE_EXPORT Exception : public std::exception {
return whatStr.c_str();
}
- operator bool() const {
+ const Core::String& toString() const throw () {
+ return errorStr;
+ }
+
+ operator bool() const throw () {
return (errorCode != SUCCESS);
}
};
diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp
index c2a90de..7282009 100644
--- a/src/Core/LogManager.cpp
+++ b/src/Core/LogManager.cpp
@@ -27,15 +27,15 @@
namespace Mad {
namespace Core {
-void LogManager::ConsoleLogger::logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) {
- if(!(level == LOG_CRITICAL && source.empty())) {// Critical messages are printed to cerr directly, so don't print them a second time
+void LogManager::ConsoleLogger::logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source) {
+ if(!(level == LOG_CRITICAL && source.isEmpty())) {// Critical messages are printed to cerr directly, so don't print them a second time
boost::lock_guard<boost::mutex> lock(cerrMutex);
logMessageDirect(category, level, timestamp, message, source);
}
}
-void LogManager::ConsoleLogger::logMessageDirect(MessageCategory /*category*/, MessageLevel /*level*/, boost::posix_time::ptime /*timestamp*/, const std::string &message, const std::string &source) {
- if(source.empty())
+void LogManager::ConsoleLogger::logMessageDirect(MessageCategory /*category*/, MessageLevel /*level*/, boost::posix_time::ptime /*timestamp*/, const Core::String &message, const Core::String &source) {
+ if(source.isEmpty())
std::cerr << message << std::endl;
else
std::cerr << message << " from " << source << std::endl;
@@ -95,7 +95,7 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
consoleLogger->setLevel(parseLevel(entry[1][0]));
}
catch(Core::Exception e) {
- application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].extract().c_str());
+ application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].toLocale().c_str());
}
return true;
@@ -104,7 +104,7 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
}
else if(entry[1].isEmpty()) {
if(!handled) {
- application->logf(Logger::LOG_WARNING, "Unknown logger '%s'.", entry[0][0].extract().c_str());
+ application->logf(Logger::LOG_WARNING, "Unknown logger '%s'.", entry[0][0].toLocale().c_str());
return true;
}
}
@@ -123,8 +123,8 @@ void LogManager::configFinished() {
queueCond.notify_one();
}
-void LogManager::log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) {
- if(level == Logger::LOG_CRITICAL && source.empty())
+void LogManager::log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source) {
+ if(level == Logger::LOG_CRITICAL && source.isEmpty())
consoleLogger->logMessageDirect(category, level, timestamp, message, source);
boost::lock_guard<boost::mutex> lock(queueMutex);
diff --git a/src/Core/LogManager.h b/src/Core/LogManager.h
index 4f7c500..a1f3d7d 100644
--- a/src/Core/LogManager.h
+++ b/src/Core/LogManager.h
@@ -51,8 +51,8 @@ class MAD_CORE_EXPORT LogManager : public Configurable {
MessageCategory category;
MessageLevel level;
boost::posix_time::ptime timestamp;
- std::string message;
- std::string source;
+ Core::String message;
+ Core::String source;
};
class ConsoleLogger : public Logger {
@@ -62,12 +62,12 @@ class MAD_CORE_EXPORT LogManager : public Configurable {
boost::mutex cerrMutex;
protected:
- virtual void logMessage(MessageCategory category, MessageLevel, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source);
+ virtual void logMessage(MessageCategory category, MessageLevel, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source);
public:
ConsoleLogger() {}
- void logMessageDirect(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source);
+ void logMessageDirect(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source);
};
@@ -102,7 +102,7 @@ class MAD_CORE_EXPORT LogManager : public Configurable {
public:
static MessageLevel parseLevel(const String &str) throw (Exception);
- void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source = std::string());
+ void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source = Core::String());
void registerLogger(boost::shared_ptr<Logger> logger) {
boost::lock_guard<boost::mutex> lock(loggerMutex);
diff --git a/src/Core/Logger.h b/src/Core/Logger.h
index a43b340..89fdc8d 100644
--- a/src/Core/Logger.h
+++ b/src/Core/Logger.h
@@ -20,8 +20,9 @@
#ifndef MAD_CORE_LOGGER_H_
#define MAD_CORE_LOGGER_H_
+#include "String.h"
+
#include <bitset>
-#include <string>
#include <boost/date_time/posix_time/ptime.hpp>
namespace Mad {
@@ -54,13 +55,13 @@ class Logger {
Logger() : level(LOG_DEFAULT), remoteLevel(LOG_DEFAULT) {setAllCategories(); setAllRemoteCategories(); remote = false;}
virtual ~Logger() {}
- void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) {
- if((source.empty() && getLevel() >= level && isCategorySet(category))
- || (!source.empty() && remote && getRemoteLevel() >= level && isRemoteCategorySet(category)))
+ void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source) {
+ if((source.isEmpty() && getLevel() >= level && isCategorySet(category))
+ || (!source.isEmpty() && remote && getRemoteLevel() >= level && isRemoteCategorySet(category)))
logMessage(category, level, timestamp, message, source);
}
- virtual void logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) = 0;
+ virtual void logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source) = 0;
public:
void setCategory(MessageCategory newCategory) {
diff --git a/src/Core/String.cpp b/src/Core/String.cpp
index d9d357e..e66e456 100644
--- a/src/Core/String.cpp
+++ b/src/Core/String.cpp
@@ -22,6 +22,15 @@
namespace Mad {
namespace Core {
+std::string String::toCodepage(const char *codepage) const {
+ boost::uint32_t len = static_cast<boost::uint32_t>(extract(0, length(), 0, codepage));
+
+ boost::scoped_array<char> buf(new char[len]);
+ extract(0, length(), buf.get(), len, codepage);
+
+ return std::string(buf.get(), len);
+}
+
boost::int32_t String::findFirstOf(const String &chars, boost::int32_t start) const {
for(boost::int32_t i = start; i < length(); ++i) {
if(chars.indexOf(charAt(i)) >= 0)
diff --git a/src/Core/String.h b/src/Core/String.h
index 1c93902..4694812 100644
--- a/src/Core/String.h
+++ b/src/Core/String.h
@@ -22,7 +22,7 @@
#include "export.h"
-#include <ostream>
+#include <iostream>
#include <string>
#include <boost/cstdint.hpp>
#include <boost/scoped_array.hpp>
@@ -35,25 +35,18 @@ namespace Core {
class MAD_CORE_EXPORT String : public icu::UnicodeString {
private:
- std::string extractCodepage(const char *codepage) const {
- boost::uint32_t len = static_cast<boost::uint32_t>(icu::UnicodeString::extract(0, length(), 0, codepage));
-
- boost::scoped_array<char> buf(new char[len]);
- icu::UnicodeString::extract(0, length(), buf.get(), len, codepage);
-
- return std::string(buf.get(), len);
- }
+ std::string toCodepage(const char *codepage) const;
public:
String() {}
- String(const icu::UnicodeString &str) : icu::UnicodeString(str) {}
- String(const char *str) : icu::UnicodeString(str, -1, US_INV) {}
+ String(const icu::UnicodeString &str) : UnicodeString(str) {}
+ String(const char *str) : UnicodeString(str, "") {}
String substr(boost::int32_t start, boost::int32_t length = -1) const {
if(length < 0)
- return icu::UnicodeString(*this, start);
+ return UnicodeString(*this, start);
else
- return icu::UnicodeString(*this, start, length);
+ return UnicodeString(*this, start, length);
}
bool matches(const String &str) const {
@@ -63,22 +56,53 @@ class MAD_CORE_EXPORT String : public icu::UnicodeString {
boost::int32_t findFirstOf(const String &chars, boost::int32_t start = 0) const;
boost::int32_t findFirstNotOf(const String &chars, boost::int32_t start = 0) const;
- std::string extract() const {
- return extractCodepage(0);
+ std::string toString() const {
+ return toCodepage("");
+ }
+
+ std::string toLocale() const {
+ return toCodepage(0);
+ }
+
+ std::string toUTF8() const {
+ return toCodepage("UTF-8");
+ }
+
+ static String fromString(const char *str) {
+ return UnicodeString(str, "");
}
- std::string extractUTF8() const {
- return extractCodepage("UTF-8");
+ static String fromString(const std::string &str) {
+ return fromString(str.c_str());
+ }
+
+ static String fromLocale(const char *str) {
+ return UnicodeString(str, static_cast<char*>(0));
+ }
+
+ static String fromLocale(const std::string &str) {
+ return fromLocale(str.c_str());
}
static String fromUTF8(const char *str) {
- return icu::UnicodeString(str, "UTF-8");
+ return UnicodeString(str, "UTF-8");
+ }
+
+ static String fromUTF8(const std::string &str) {
+ return fromUTF8(str.c_str());
+ }
+
+ template<typename ElemType, typename Traits>
+ static String getline(std::basic_istream<ElemType, Traits> &stream) {
+ std::string input;
+ std::getline(stream, input);
+ return fromLocale(input);
}
};
template<typename ElemType, typename Traits>
std::basic_ostream<ElemType, Traits>& operator<<(std::basic_ostream<ElemType, Traits> &stream, const String &string) {
- stream << string.extract();
+ stream << string.toLocale();
return stream;
}