diff --git a/CMakeLists.txt b/CMakeLists.txt index dc872d29c..0f86048b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ endif() if(NOT MSVC) add_definitions( - -std=c++11 + -std=c++14 -Wall -nostdlib -fno-exceptions diff --git a/src/ox/CMakeLists.txt b/src/ox/CMakeLists.txt index a3968a6fc..3dcead2c4 100644 --- a/src/ox/CMakeLists.txt +++ b/src/ox/CMakeLists.txt @@ -4,5 +4,6 @@ if(OX_USE_STDLIB STREQUAL "ON") add_subdirectory(clargs) endif(OX_USE_STDLIB STREQUAL "ON") add_subdirectory(fs) +add_subdirectory(log) add_subdirectory(mc) add_subdirectory(std) diff --git a/src/ox/fs/CMakeLists.txt b/src/ox/fs/CMakeLists.txt index 42ca4b256..5c3332179 100644 --- a/src/ox/fs/CMakeLists.txt +++ b/src/ox/fs/CMakeLists.txt @@ -23,6 +23,7 @@ if(OX_BUILD_EXEC STREQUAL "ON") target_link_libraries( oxfstool OxFS + OxLog OxStd ) endif() diff --git a/src/ox/fs/test/CMakeLists.txt b/src/ox/fs/test/CMakeLists.txt index be393710a..c6454ef86 100644 --- a/src/ox/fs/test/CMakeLists.txt +++ b/src/ox/fs/test/CMakeLists.txt @@ -20,10 +20,33 @@ add_executable( tests.cpp ) -target_link_libraries(FileStoreFormat OxFS OxStd) -target_link_libraries(FileSystemFormat OxFS OxStd) -target_link_libraries(FileStoreIO OxFS OxStd) -target_link_libraries(FSTests OxFS OxStd) +target_link_libraries( + FileStoreFormat + OxFS + OxStd + OxLog +) + +target_link_libraries( + FileSystemFormat + OxFS + OxStd + OxLog +) + +target_link_libraries( + FileStoreIO + OxFS + OxStd + OxLog +) + +target_link_libraries( + FSTests + OxFS + OxStd + OxLog +) add_test("FileStoreFormat" FileStoreFormat) add_test("FileSystemFormat" FileSystemFormat) diff --git a/src/ox/log/CMakeLists.txt b/src/ox/log/CMakeLists.txt new file mode 100644 index 000000000..ca9dfcc45 --- /dev/null +++ b/src/ox/log/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 2.8) + +add_library( + OxLog + log.cpp +) + +set_property( + TARGET + OxLog + PROPERTY + POSITION_INDEPENDENT_CODE ON +) + +install( + FILES + log.hpp + DESTINATION + include/ox/mc +) + +install(TARGETS OxLog + LIBRARY DESTINATION lib/ox + ARCHIVE DESTINATION lib/ox +) diff --git a/src/ox/log/log.cpp b/src/ox/log/log.cpp new file mode 100644 index 000000000..0624fd0dc --- /dev/null +++ b/src/ox/log/log.cpp @@ -0,0 +1,105 @@ +/* + * Copyright 2015 - 2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +#include "log.hpp" + +namespace ox { + +class Logger { + + private: + // void* stand-in for FILE* + const char *m_path = nullptr; + + public: + Logger(const char *path = nullptr); + + ~Logger(); + + void log(LogLevel_t level, const char *msg, va_list args); + + void info(const char *msg, ...); + + void debug(const char *msg, ...); + + void error(const char *msg, ...); +}; + +Logger::Logger(const char *path) { + m_path = path; +} + +Logger::~Logger() { +} + +void Logger::log(LogLevel_t level, const char *msg, va_list args) { + if (m_path) { + auto file = fopen(m_path, "a"); + vfprintf(file, msg, args); + fprintf(file, "\n"); + fclose(file); + } +} + +void Logger::info(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Info, msg, args); + va_end(args); +} + +void Logger::debug(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Debug, msg, args); + va_end(args); +} + +void Logger::error(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Error, msg, args); + va_end(args); +} + + +static Logger logger; + +void logFile(const char *path) { + logger = Logger(path); +} + +void log(LogLevel_t level, const char *msg, va_list args) { + logger.log(level, msg, args); +} + +void info(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Info, msg, args); + va_end(args); +} + +void debug(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Debug, msg, args); + va_end(args); +} + +void error(const char *msg, ...) { + va_list args; + va_start(args, msg); + log(LogLevel_t::Error, msg, args); + va_end(args); +} + +} diff --git a/src/ox/log/log.hpp b/src/ox/log/log.hpp new file mode 100644 index 000000000..29b7ccf34 --- /dev/null +++ b/src/ox/log/log.hpp @@ -0,0 +1,27 @@ +/* + * Copyright 2015 - 2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +namespace ox { + +enum class LogLevel_t: int { + Info, + Debug, + Error, +}; + +void logFile(const char *path); + +void info(const char *msg, ...); + +void debug(const char *msg, ...); + +void error(const char *msg, ...); + +} diff --git a/src/ox/mc/test/CMakeLists.txt b/src/ox/mc/test/CMakeLists.txt index 97544fad5..268481915 100644 --- a/src/ox/mc/test/CMakeLists.txt +++ b/src/ox/mc/test/CMakeLists.txt @@ -5,7 +5,12 @@ add_executable( tests.cpp ) -target_link_libraries(McTest OxStd OxMetalClaw) +target_link_libraries( + McTest + OxMetalClaw + OxStd + OxLog +) add_test("Test\\ McTest\\ Writer" McTest MetalClawWriter) add_test("Test\\ McTest\\ Reader" McTest MetalClawReader) diff --git a/src/ox/std/test/CMakeLists.txt b/src/ox/std/test/CMakeLists.txt index da900ca6c..1b25b3ba8 100644 --- a/src/ox/std/test/CMakeLists.txt +++ b/src/ox/std/test/CMakeLists.txt @@ -5,7 +5,7 @@ add_executable( tests.cpp ) -target_link_libraries(StdTest OxStd) +target_link_libraries(StdTest OxLog OxStd) add_test("Test\\ ox_memcmp\\ ABCDEFG\\ !=\\ HIJKLMN" StdTest "ABCDEFG != HIJKLMN") add_test("Test\\ ox_memcmp\\ HIJKLMN\\ !=\\ ABCDEFG" StdTest "HIJKLMN != ABCDEFG") @@ -21,14 +21,18 @@ add_executable( strops_test.cpp ) -target_link_libraries(StrOpsTest OxStd) +target_link_libraries( + StrOpsTest + OxStd + OxLog +) add_test("Test\\ ox_strcmp\\ asdf\\ !=\\ hijk" StrOpsTest "asdf < hijk") add_test("Test\\ ox_strcmp\\ hijk\\ !=\\ asdf" StrOpsTest "hijk > asdf") add_test("Test\\ ox_strcmp\\ read\\ !=\\ resize" StrOpsTest "read < resize") add_test("Test\\ ox_strcmp\\ resize\\ !=\\ read" StrOpsTest "resize > read") add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest "resize == resize") -add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest " == ") +add_test("Test\\ ox_strcmp\\ ''\\ ==\\ ''" StrOpsTest " == ") add_test("Test\\ ox_strchr\\ 0" StrOpsTest "ox_strchr 0") add_test("Test\\ ox_lastIndexOf\\ aaaa\\ a" StrOpsTest "ox_lastIndexOf aaaa a") @@ -41,7 +45,11 @@ add_executable( byteswap_test.cpp ) -target_link_libraries(ByteSwapTest OxStd) +target_link_libraries( + ByteSwapTest + OxStd + OxLog +) add_test("Test\\ bigEndianAdapt\\ 0x00ff" ByteSwapTest bigEndianAdapt 0x00ff) add_test("Test\\ bigEndianAdapt\\ 0xff00" ByteSwapTest bigEndianAdapt 0xff00)