Move LOG() to base library

Changes:
- Moved verbose_log (in app module) to base_log (in base library)
- Added base_set_log_filename() to start logging and close the log file
- LoggerModule() is implemented using thse new base library functions

Fix #899
This commit is contained in:
David Capello 2015-12-21 12:36:44 -03:00
parent 3bcda8c1b1
commit 7ab863303e
5 changed files with 88 additions and 51 deletions

View File

@ -13,67 +13,32 @@
#include "app/app.h" #include "app/app.h"
#include "app/resource_finder.h" #include "app/resource_finder.h"
#include "ui/base.h" #include "base/log.h"
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <string>
namespace app { namespace app {
static FILE* log_fileptr = NULL;
static LoggerModule* logger_instance = NULL; static LoggerModule* logger_instance = NULL;
LoggerModule::LoggerModule(bool verbose) LoggerModule::LoggerModule(bool verbose)
: m_verbose(verbose) : m_verbose(verbose)
{ {
logger_instance = this; logger_instance = this;
if (verbose) {
app::ResourceFinder rf(false);
rf.includeUserDir("aseprite.log");
auto filename = rf.defaultFilename();
base_set_log_filename(filename.c_str());
}
} }
LoggerModule::~LoggerModule() LoggerModule::~LoggerModule()
{ {
LOG("Logger module: shutting down (this is the last line)\n"); LOG("Logger module: shutting down (this is the last line)\n");
if (log_fileptr) { // Close log file
fclose(log_fileptr); base_set_log_filename("");
log_fileptr = NULL; logger_instance = nullptr;
}
logger_instance = NULL;
} }
} // namespace app } // namespace app
void verbose_log(const char* format, ...)
{
if (app::logger_instance && !app::logger_instance->isVerbose())
return;
if (!app::log_fileptr) {
std::string filename;
app::ResourceFinder rf(false);
rf.includeUserDir("aseprite.log");
filename = rf.defaultFilename();
if (filename.size() > 0)
app::log_fileptr = fopen(filename.c_str(), "w");
}
if (app::log_fileptr) {
va_list ap;
va_start(ap, format);
vfprintf(app::log_fileptr, format, ap);
fflush(app::log_fileptr);
#ifdef _DEBUG
va_start(ap, format);
vfprintf(stderr, format, ap);
fflush(stderr);
#endif
va_end(ap);
}
}

View File

@ -51,6 +51,7 @@ set(BASE_SOURCES
file_handle.cpp file_handle.cpp
fs.cpp fs.cpp
launcher.cpp launcher.cpp
log.cpp
mem_utils.cpp mem_utils.cpp
memory.cpp memory.cpp
memory_dump.cpp memory_dump.cpp

56
src/base/log.cpp Normal file
View File

@ -0,0 +1,56 @@
// Aseprite Base Library
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/log.h"
#include "base/file_handle.h"
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <string>
static FILE* log_fileptr = nullptr;
static std::string log_filename;
void base_set_log_filename(const char* filename)
{
if (log_fileptr) {
fclose(log_fileptr);
log_fileptr = nullptr;
}
log_filename = filename;
}
void base_log(const char* format, ...)
{
if (!log_fileptr) {
if (log_filename.empty())
return;
log_fileptr = base::open_file_raw(log_filename, "w");
}
if (log_fileptr) {
va_list ap;
va_start(ap, format);
vfprintf(log_fileptr, format, ap);
fflush(log_fileptr);
#ifdef _DEBUG
va_start(ap, format);
vfprintf(stderr, format, ap);
fflush(stderr);
#endif
va_end(ap);
}
}

19
src/base/log.h Normal file
View File

@ -0,0 +1,19 @@
// Aseprite Base Library
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef BASE_LOG_H_INCLUDED
#define BASE_LOG_H_INCLUDED
#pragma once
// Define BASE_DONT_DEFINE_LOG_MACRO in case that you don't need LOG
#ifndef BASE_DONT_DEFINE_LOG_MACRO
#define LOG base_log
#endif
void base_set_log_filename(const char* filename);
void base_log(const char* format, ...);
#endif

View File

@ -35,10 +35,6 @@
#define UPDATE_URL WEBSITE "update/?xml=1" #define UPDATE_URL WEBSITE "update/?xml=1"
#define COPYRIGHT "Copyright (C) 2001-2015 David Capello" #define COPYRIGHT "Copyright (C) 2001-2015 David Capello"
#define LOG verbose_log
// verbose_log() is defined in src/app/log.cpp and used through LOG macro
void verbose_log(const char* format, ...);
#include "base/base.h" #include "base/base.h"
#include "base/debug.h" #include "base/debug.h"
#include "base/log.h"