From 7ab863303e62244a249f39af2651dbcd457ded96 Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 21 Dec 2015 12:36:44 -0300 Subject: [PATCH] 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 --- src/app/log.cpp | 57 ++++++++--------------------------------- src/base/CMakeLists.txt | 1 + src/base/log.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ src/base/log.h | 19 ++++++++++++++ src/config.h | 6 +---- 5 files changed, 88 insertions(+), 51 deletions(-) create mode 100644 src/base/log.cpp create mode 100644 src/base/log.h diff --git a/src/app/log.cpp b/src/app/log.cpp index d3ec160a2..e7e41fbc4 100644 --- a/src/app/log.cpp +++ b/src/app/log.cpp @@ -13,67 +13,32 @@ #include "app/app.h" #include "app/resource_finder.h" -#include "ui/base.h" - -#include -#include -#include -#include +#include "base/log.h" namespace app { -static FILE* log_fileptr = NULL; static LoggerModule* logger_instance = NULL; LoggerModule::LoggerModule(bool verbose) : m_verbose(verbose) { 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() { LOG("Logger module: shutting down (this is the last line)\n"); - if (log_fileptr) { - fclose(log_fileptr); - log_fileptr = NULL; - } - - logger_instance = NULL; + // Close log file + base_set_log_filename(""); + logger_instance = nullptr; } } // 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); - } - -} diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index ce55e306a..407da210a 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -51,6 +51,7 @@ set(BASE_SOURCES file_handle.cpp fs.cpp launcher.cpp + log.cpp mem_utils.cpp memory.cpp memory_dump.cpp diff --git a/src/base/log.cpp b/src/base/log.cpp new file mode 100644 index 000000000..74ebab015 --- /dev/null +++ b/src/base/log.cpp @@ -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 +#include +#include +#include + +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); + } +} diff --git a/src/base/log.h b/src/base/log.h new file mode 100644 index 000000000..0eff40123 --- /dev/null +++ b/src/base/log.h @@ -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 diff --git a/src/config.h b/src/config.h index d6e278f4f..a094ff6c4 100644 --- a/src/config.h +++ b/src/config.h @@ -35,10 +35,6 @@ #define UPDATE_URL WEBSITE "update/?xml=1" #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/debug.h" +#include "base/log.h"