Switch from Nz prefix to namespace Nz
What a huge commit Former-commit-id: 38ac5eebf70adc1180f571f6006192d28fb99897
This commit is contained in:
@@ -21,142 +21,145 @@
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace
|
||||
namespace Nz
|
||||
{
|
||||
const char* errorType[] = {
|
||||
"Assert failed: ", // nzErrorType_AssertFailed
|
||||
"Internal error: ", // nzErrorType_Internal
|
||||
"Error: ", // nzErrorType_Normal
|
||||
"Warning: " // nzErrorType_Warning
|
||||
};
|
||||
namespace
|
||||
{
|
||||
const char* errorType[] = {
|
||||
"Assert failed: ", // ErrorType_AssertFailed
|
||||
"Internal error: ", // ErrorType_Internal
|
||||
"Error: ", // ErrorType_Normal
|
||||
"Warning: " // ErrorType_Warning
|
||||
};
|
||||
|
||||
static_assert(sizeof(errorType)/sizeof(const char*) == nzErrorType_Max+1, "Error type array is incomplete");
|
||||
}
|
||||
static_assert(sizeof(errorType)/sizeof(const char*) == ErrorType_Max+1, "Error type array is incomplete");
|
||||
}
|
||||
|
||||
NzLog::NzLog() :
|
||||
m_filePath("NazaraLog.log"),
|
||||
m_file(nullptr),
|
||||
m_append(false),
|
||||
m_enabled(true),
|
||||
m_writeTime(true)
|
||||
{
|
||||
}
|
||||
Log::Log() :
|
||||
m_filePath("NazaraLog.log"),
|
||||
m_file(nullptr),
|
||||
m_append(false),
|
||||
m_enabled(true),
|
||||
m_writeTime(true)
|
||||
{
|
||||
}
|
||||
|
||||
NzLog::~NzLog()
|
||||
{
|
||||
delete m_file;
|
||||
}
|
||||
|
||||
void NzLog::Enable(bool enable)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
if (m_enabled == enable)
|
||||
return;
|
||||
|
||||
m_enabled = enable;
|
||||
if (!m_enabled && m_file)
|
||||
Log::~Log()
|
||||
{
|
||||
delete m_file;
|
||||
m_file = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void NzLog::EnableAppend(bool enable)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
m_append = enable;
|
||||
if (!m_append && m_file)
|
||||
void Log::Enable(bool enable)
|
||||
{
|
||||
m_file->Delete();
|
||||
m_file = nullptr;
|
||||
}
|
||||
}
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
void NzLog::EnableDateTime(bool enable)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
if (m_enabled == enable)
|
||||
return;
|
||||
|
||||
m_writeTime = enable;
|
||||
}
|
||||
|
||||
NzString NzLog::GetFile() const
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
if (m_file)
|
||||
return m_file->GetPath();
|
||||
else
|
||||
return NzString();
|
||||
}
|
||||
|
||||
bool NzLog::IsEnabled() const
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void NzLog::SetFile(const NzString& filePath)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
m_filePath = filePath;
|
||||
if (m_file)
|
||||
m_file->SetFile(filePath);
|
||||
}
|
||||
|
||||
void NzLog::Write(const NzString& string)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
if (m_enabled)
|
||||
{
|
||||
if (!m_file)
|
||||
m_file = new NzFile(m_filePath, nzOpenMode_Text | nzOpenMode_WriteOnly | ((m_append) ? nzOpenMode_Append : nzOpenMode_Truncate));
|
||||
|
||||
NzString line;
|
||||
|
||||
if (m_writeTime)
|
||||
m_enabled = enable;
|
||||
if (!m_enabled && m_file)
|
||||
{
|
||||
line.Reserve(23 + string.GetSize() + 1);
|
||||
line.Set(23, '\0'); // Buffer non-initialisé
|
||||
|
||||
time_t currentTime = std::time(nullptr);
|
||||
std::strftime(&line[0], 24, "%d/%m/%Y - %H:%M:%S: ", std::localtime(¤tTime));
|
||||
delete m_file;
|
||||
m_file = nullptr;
|
||||
}
|
||||
else
|
||||
line.Reserve(string.GetSize() + 1);
|
||||
|
||||
line += string;
|
||||
line += '\n';
|
||||
|
||||
if (m_file->IsOpen())
|
||||
m_file->Write(line);
|
||||
|
||||
#if NAZARA_CORE_DUPLICATE_LOG_TO_COUT
|
||||
std::fputs(line.GetConstBuffer(), stdout);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void NzLog::WriteError(nzErrorType type, const NzString& error)
|
||||
{
|
||||
NzStringStream stream;
|
||||
stream << errorType[type] << error;
|
||||
Write(stream);
|
||||
}
|
||||
void Log::EnableAppend(bool enable)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
void NzLog::WriteError(nzErrorType type, const NzString& error, unsigned int line, const NzString& file, const NzString& func)
|
||||
{
|
||||
NzStringStream stream;
|
||||
stream << errorType[type] << error << " (" << file << ':' << line << ": " << func << ')';
|
||||
Write(stream);
|
||||
}
|
||||
m_append = enable;
|
||||
if (!m_append && m_file)
|
||||
{
|
||||
m_file->Delete();
|
||||
m_file = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
NzLog* NzLog::Instance()
|
||||
{
|
||||
static NzLog log;
|
||||
return &log;
|
||||
void Log::EnableDateTime(bool enable)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
m_writeTime = enable;
|
||||
}
|
||||
|
||||
String Log::GetFile() const
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
if (m_file)
|
||||
return m_file->GetPath();
|
||||
else
|
||||
return String();
|
||||
}
|
||||
|
||||
bool Log::IsEnabled() const
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void Log::SetFile(const String& filePath)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
m_filePath = filePath;
|
||||
if (m_file)
|
||||
m_file->SetFile(filePath);
|
||||
}
|
||||
|
||||
void Log::Write(const String& string)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
if (m_enabled)
|
||||
{
|
||||
if (!m_file)
|
||||
m_file = new File(m_filePath, OpenMode_Text | OpenMode_WriteOnly | ((m_append) ? OpenMode_Append : OpenMode_Truncate));
|
||||
|
||||
String line;
|
||||
|
||||
if (m_writeTime)
|
||||
{
|
||||
line.Reserve(23 + string.GetSize() + 1);
|
||||
line.Set(23, '\0'); // Buffer non-initialisé
|
||||
|
||||
time_t currentTime = std::time(nullptr);
|
||||
std::strftime(&line[0], 24, "%d/%m/%Y - %H:%M:%S: ", std::localtime(¤tTime));
|
||||
}
|
||||
else
|
||||
line.Reserve(string.GetSize() + 1);
|
||||
|
||||
line += string;
|
||||
line += '\n';
|
||||
|
||||
if (m_file->IsOpen())
|
||||
m_file->Write(line);
|
||||
|
||||
#if NAZARA_CORE_DUPLICATE_LOG_TO_COUT
|
||||
std::fputs(line.GetConstBuffer(), stdout);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void Log::WriteError(ErrorType type, const String& error)
|
||||
{
|
||||
StringStream stream;
|
||||
stream << errorType[type] << error;
|
||||
Write(stream);
|
||||
}
|
||||
|
||||
void Log::WriteError(ErrorType type, const String& error, unsigned int line, const String& file, const String& func)
|
||||
{
|
||||
StringStream stream;
|
||||
stream << errorType[type] << error << " (" << file << ':' << line << ": " << func << ')';
|
||||
Write(stream);
|
||||
}
|
||||
|
||||
Log* Log::Instance()
|
||||
{
|
||||
static Log log;
|
||||
return &log;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user