From 7d5f055547e70fa93ee9ac944e62f8d657b9dc55 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 19 Oct 2018 08:36:44 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Utils/Log.cpp | 2 - src/libjin/Utils/Log.h | 134 -------------------------------------- src/libjin/Utils/endian.h | 23 ------- src/libjin/Utils/je_endian.h | 23 +++++++ src/libjin/Utils/je_log.cpp | 2 + src/libjin/Utils/je_log.h | 135 +++++++++++++++++++++++++++++++++++++++ src/libjin/Utils/je_macros.h | 17 +++++ src/libjin/Utils/je_unittest.cpp | 108 +++++++++++++++++++++++++++++++ src/libjin/Utils/je_utils.h | 9 +++ src/libjin/Utils/log.h | 134 -------------------------------------- src/libjin/Utils/macros.h | 17 ----- src/libjin/Utils/unittest.cpp | 108 ------------------------------- src/libjin/Utils/utils.h | 9 --- 13 files changed, 294 insertions(+), 427 deletions(-) delete mode 100644 src/libjin/Utils/Log.cpp delete mode 100644 src/libjin/Utils/Log.h delete mode 100644 src/libjin/Utils/endian.h create mode 100644 src/libjin/Utils/je_endian.h create mode 100644 src/libjin/Utils/je_log.cpp create mode 100644 src/libjin/Utils/je_log.h create mode 100644 src/libjin/Utils/je_macros.h create mode 100644 src/libjin/Utils/je_unittest.cpp create mode 100644 src/libjin/Utils/je_utils.h delete mode 100644 src/libjin/Utils/log.h delete mode 100644 src/libjin/Utils/macros.h delete mode 100644 src/libjin/Utils/unittest.cpp delete mode 100644 src/libjin/Utils/utils.h (limited to 'src/libjin/Utils') diff --git a/src/libjin/Utils/Log.cpp b/src/libjin/Utils/Log.cpp deleted file mode 100644 index 5299942..0000000 --- a/src/libjin/Utils/Log.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define LOGHELPER_IMPLEMENT -#include "log.h" \ No newline at end of file diff --git a/src/libjin/Utils/Log.h b/src/libjin/Utils/Log.h deleted file mode 100644 index e4ed879..0000000 --- a/src/libjin/Utils/Log.h +++ /dev/null @@ -1,134 +0,0 @@ -/** -* Single.h/loghelper.h -* Copyright (C) 2017~2018 chai -*/ -#ifndef __LOG_HELPER_H -#define __LOG_HELPER_H -#include -#include -#include -#include - -class Loghelper -{ -public: - // log输出目标 - enum Direction - { - DIR_CERR = 1 << 1, // 标准错误流 - DIR_FILE = 1 << 2, // log文件 - }; - - // 错误等级 - enum Level - { - LV_NONE = 0, // none - LV_ERROR = 1 << 1, // error - LV_WARN = 1 << 2, // warn - LV_INFO = 1 << 3, // info - LV_DEBUG = 1 << 4, // debug - LV_ALL = 0xffffffff - }; - - static void log(Level _level, const char* _fmt, ...); - - // 重定向 - static void redirect(unsigned int _dir, char* _path = nullptr); - - // 筛选错误等级 - static void restrict(unsigned int levels); - - static void close(); - -private: - static unsigned int dir; // 输出目标 - static unsigned int levels; // 错误等级 - static std::ofstream fs; // 输出文件流 -}; - -typedef Loghelper::Level Loglevel; - -#ifdef LOGHELPER_IMPLEMENT - -#define hasbit(flag, bit) ((flag & bit) == bit) - -unsigned int Loghelper::dir = Loghelper::Direction::DIR_CERR; -unsigned int Loghelper::levels = Loghelper::Level::LV_ALL; -std::ofstream Loghelper::fs; - -void Loghelper::log(Level _level, const char* _fmt, ...) -{ - if (!hasbit(levels, _level)) - return; -#define FORMAT_MSG_BUFFER_SIZE (204800) - const char* levelStr = nullptr; - switch (_level) - { - case LV_ERROR: - levelStr = "[Jin Error]:"; - break; - case LV_WARN: - levelStr = "[Jin Warn]:"; - break; - case LV_INFO: - levelStr = "[Jin Info]:"; - break; - case LV_DEBUG: - levelStr = "[Jin Debug]:"; - break; - default: - levelStr = "[Jin Unknown]:"; - break; - } - char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; - strcpy(buffer, levelStr); - va_list args; - va_start(args, _fmt); - vsnprintf(buffer + strlen(buffer), FORMAT_MSG_BUFFER_SIZE, _fmt, args); - va_end(args); - if (hasbit(dir, DIR_CERR)) - { - std::cerr << buffer << std::endl; - } - if (hasbit(dir, DIR_FILE)) - { - fs << buffer << std::endl; - } -#undef FORMAT_MSG_BUFFER_SIZE -} - -// 重定向 -void Loghelper::redirect(unsigned int _dir, char* _path) -{ - dir = _dir; - if (hasbit(dir, DIR_FILE)) - { - try - { - fs.open(_path, std::ios_base::app); - } - catch (std::ios_base::failure& e) { - dir = DIR_CERR; - log(Level::LV_WARN, "重定向log路径 %s 失败", _path); - } - } -} - -// 筛选错误等级 -void Loghelper::restrict(unsigned int _levels) -{ - levels = _levels; -} - -void Loghelper::close() -{ - if (!fs.fail()) - fs.close(); - fs.clear(); -} - -#undef hasbit - -#endif - -#endif diff --git a/src/libjin/Utils/endian.h b/src/libjin/Utils/endian.h deleted file mode 100644 index 01def88..0000000 --- a/src/libjin/Utils/endian.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef LIBJIN_LIL_ENDIAN && LIBJIN_BIG_ENDIAN - -#define LIBJIN_LIL_ENDIAN 2 -#define LIBJIN_BIG_ENDIAN 4 - -#endif - -#ifndef LIBJIN_BYTEORDER -#ifdef __linux__ -#include -#define LIBJIN_BYTEORDER __BYTE_ORDER -#else /* __linux__ */ -#if defined(__hppa__) || \ - defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \ - (defined(__MIPS__) && defined(__MISPEB__)) || \ - defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \ - defined(__sparc__) -#define LIBJIN_BYTEORDER LIBJIN_BIG_ENDIAN -#else -#define LIBJIN_BYTEORDER LIBJIN_LIL_ENDIAN -#endif -#endif /* __linux__ */ -#endif /* !SDL_BYTEORDER */ \ No newline at end of file diff --git a/src/libjin/Utils/je_endian.h b/src/libjin/Utils/je_endian.h new file mode 100644 index 0000000..01def88 --- /dev/null +++ b/src/libjin/Utils/je_endian.h @@ -0,0 +1,23 @@ +#ifndef LIBJIN_LIL_ENDIAN && LIBJIN_BIG_ENDIAN + +#define LIBJIN_LIL_ENDIAN 2 +#define LIBJIN_BIG_ENDIAN 4 + +#endif + +#ifndef LIBJIN_BYTEORDER +#ifdef __linux__ +#include +#define LIBJIN_BYTEORDER __BYTE_ORDER +#else /* __linux__ */ +#if defined(__hppa__) || \ + defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \ + (defined(__MIPS__) && defined(__MISPEB__)) || \ + defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \ + defined(__sparc__) +#define LIBJIN_BYTEORDER LIBJIN_BIG_ENDIAN +#else +#define LIBJIN_BYTEORDER LIBJIN_LIL_ENDIAN +#endif +#endif /* __linux__ */ +#endif /* !SDL_BYTEORDER */ \ No newline at end of file diff --git a/src/libjin/Utils/je_log.cpp b/src/libjin/Utils/je_log.cpp new file mode 100644 index 0000000..2bcb25a --- /dev/null +++ b/src/libjin/Utils/je_log.cpp @@ -0,0 +1,2 @@ +#define LOGHELPER_IMPLEMENT +#include "je_log.h" \ No newline at end of file diff --git a/src/libjin/Utils/je_log.h b/src/libjin/Utils/je_log.h new file mode 100644 index 0000000..9b3fe2c --- /dev/null +++ b/src/libjin/Utils/je_log.h @@ -0,0 +1,135 @@ +/** +* Single.h/loghelper.h +* Copyright (C) 2017~2018 chai +*/ +#ifndef __LOG_HELPER_H +#define __LOG_HELPER_H + +#include +#include +#include +#include + +class Loghelper +{ +public: + // log输出目标 + enum Direction + { + DIR_CERR = 1 << 1, // 标准错误流 + DIR_FILE = 1 << 2, // log文件 + }; + + // 错误等级 + enum Level + { + LV_NONE = 0, // none + LV_ERROR = 1 << 1, // error + LV_WARN = 1 << 2, // warn + LV_INFO = 1 << 3, // info + LV_DEBUG = 1 << 4, // debug + LV_ALL = 0xffffffff + }; + + static void log(Level _level, const char* _fmt, ...); + + // 重定向 + static void redirect(unsigned int _dir, char* _path = nullptr); + + // 筛选错误等级 + static void restrict(unsigned int levels); + + static void close(); + +private: + static unsigned int dir; // 输出目标 + static unsigned int levels; // 错误等级 + static std::ofstream fs; // 输出文件流 +}; + +typedef Loghelper::Level Loglevel; + +#ifdef LOGHELPER_IMPLEMENT + +#define hasbit(flag, bit) ((flag & bit) == bit) + +unsigned int Loghelper::dir = Loghelper::Direction::DIR_CERR; +unsigned int Loghelper::levels = Loghelper::Level::LV_ALL; +std::ofstream Loghelper::fs; + +void Loghelper::log(Level _level, const char* _fmt, ...) +{ + if (!hasbit(levels, _level)) + return; +#define FORMAT_MSG_BUFFER_SIZE (204800) + const char* levelStr = nullptr; + switch (_level) + { + case LV_ERROR: + levelStr = "[Jin Error]:"; + break; + case LV_WARN: + levelStr = "[Jin Warn]:"; + break; + case LV_INFO: + levelStr = "[Jin Info]:"; + break; + case LV_DEBUG: + levelStr = "[Jin Debug]:"; + break; + default: + levelStr = "[Jin Unknown]:"; + break; + } + char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; + strcpy(buffer, levelStr); + va_list args; + va_start(args, _fmt); + vsnprintf(buffer + strlen(buffer), FORMAT_MSG_BUFFER_SIZE, _fmt, args); + va_end(args); + if (hasbit(dir, DIR_CERR)) + { + std::cerr << buffer << std::endl; + } + if (hasbit(dir, DIR_FILE)) + { + fs << buffer << std::endl; + } +#undef FORMAT_MSG_BUFFER_SIZE +} + +// 重定向 +void Loghelper::redirect(unsigned int _dir, char* _path) +{ + dir = _dir; + if (hasbit(dir, DIR_FILE)) + { + try + { + fs.open(_path, std::ios_base::app); + } + catch (std::ios_base::failure& e) { + dir = DIR_CERR; + log(Level::LV_WARN, "重定向log路径 %s 失败", _path); + } + } +} + +// 筛选错误等级 +void Loghelper::restrict(unsigned int _levels) +{ + levels = _levels; +} + +void Loghelper::close() +{ + if (!fs.fail()) + fs.close(); + fs.clear(); +} + +#undef hasbit + +#endif + +#endif diff --git a/src/libjin/Utils/je_macros.h b/src/libjin/Utils/je_macros.h new file mode 100644 index 0000000..e19193c --- /dev/null +++ b/src/libjin/Utils/je_macros.h @@ -0,0 +1,17 @@ +#ifndef __LIBJIN_MACROS_H +#define __LIBJIN_MACROS_H +#include + +//#define implement // 实现接口 +// +//#define shared // 声明类的方法 +// +//#define MASK // 声明掩码 enum +// +//#define onlyonce // 声明只会调用一次 +//#define CALLONCE(call) static char __dummy__=(call, 1) // 只会调用一次 +//#define SAFECALL(func, params) if(func) func(params) +// +//#define zero(mem) memset(&mem, 0, sizeof(mem)) + +#endif \ No newline at end of file diff --git a/src/libjin/Utils/je_unittest.cpp b/src/libjin/Utils/je_unittest.cpp new file mode 100644 index 0000000..65dbaad --- /dev/null +++ b/src/libjin/Utils/je_unittest.cpp @@ -0,0 +1,108 @@ +#include "je_utils.h" +#if UNITTEST + +#include +#include +#include +#include "../audio/sdl/source.h" +#include "../audio/sdl/audio.h" + +using namespace jin::audio; +using namespace std; + +int main(int argc, char* argv[]) +{ + SDLAudio* audio = SDLAudio::get(); + audio->init(0); + SDLSource* source = SDLSource::createSource("a.ogg"); + SDLSource* source2 = SDLSource::createSource("a.wav"); + //source->play(); + source2->play(); + source->setLoop(true); + source2->setLoop(true); + int i = 0; + while (true) + { + SDL_Delay(1000); + } + audio->quit(); + return 0; +} + +/* +#include +#include +#include +#include "SDL2/SDL.h" + +#include <3rdparty/cmixer/cmixer.h> + +static SDL_mutex* audio_mutex; + +static void lock_handler(cm_Event *e) { + if (e->type == CM_EVENT_LOCK) { + SDL_LockMutex(audio_mutex); + } + if (e->type == CM_EVENT_UNLOCK) { + SDL_UnlockMutex(audio_mutex); + } +} + + +static void audio_callback(void *udata, Uint8 *stream, int size) { + cm_process((cm_Int16*)stream, size / 2); +} + + +int main(int argc, char **argv) { + SDL_AudioDeviceID dev; + SDL_AudioSpec fmt, got; + cm_Source *src; + cm_Source* src2; + + + SDL_Init(SDL_INIT_AUDIO); + audio_mutex = SDL_CreateMutex(); + + memset(&fmt, 0, sizeof(fmt)); + fmt.freq = 44100; + fmt.format = AUDIO_S16; + fmt.channels = 2; + fmt.samples = 1024; + fmt.callback = audio_callback; + + dev = SDL_OpenAudioDevice(NULL, 0, &fmt, &got, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); + if (dev == 0) { + fprintf(stderr, "Error: failed to open audio device '%s'\n", SDL_GetError()); + exit(EXIT_FAILURE); + } + + cm_init(got.freq); + cm_set_lock(lock_handler); + cm_set_master_gain(0.5); + + SDL_PauseAudioDevice(dev, 0); + + src = cm_new_source_from_file("a.ogg"); + src2 = cm_new_source_from_file("loop.wav"); + if (!src) { + fprintf(stderr, "Error: failed to create source '%s'\n", cm_get_error()); + exit(EXIT_FAILURE); + } + cm_set_loop(src2, 1); + + cm_play(src); + cm_play(src2); + + printf("Press [return] to exit\n"); + getchar(); + + cm_destroy_source(src); + SDL_CloseAudioDevice(dev); + SDL_Quit(); + + return EXIT_SUCCESS; +} +*/ + +#endif \ No newline at end of file diff --git a/src/libjin/Utils/je_utils.h b/src/libjin/Utils/je_utils.h new file mode 100644 index 0000000..583f892 --- /dev/null +++ b/src/libjin/Utils/je_utils.h @@ -0,0 +1,9 @@ +#ifndef __LIBJIN_UTILS_H +#define __LIBJIN_UTILS_H + +#include "je_macros.h" +#include "je_endian.h" + +#define UNITTEST 0 + +#endif \ No newline at end of file diff --git a/src/libjin/Utils/log.h b/src/libjin/Utils/log.h deleted file mode 100644 index e4ed879..0000000 --- a/src/libjin/Utils/log.h +++ /dev/null @@ -1,134 +0,0 @@ -/** -* Single.h/loghelper.h -* Copyright (C) 2017~2018 chai -*/ -#ifndef __LOG_HELPER_H -#define __LOG_HELPER_H -#include -#include -#include -#include - -class Loghelper -{ -public: - // log输出目标 - enum Direction - { - DIR_CERR = 1 << 1, // 标准错误流 - DIR_FILE = 1 << 2, // log文件 - }; - - // 错误等级 - enum Level - { - LV_NONE = 0, // none - LV_ERROR = 1 << 1, // error - LV_WARN = 1 << 2, // warn - LV_INFO = 1 << 3, // info - LV_DEBUG = 1 << 4, // debug - LV_ALL = 0xffffffff - }; - - static void log(Level _level, const char* _fmt, ...); - - // 重定向 - static void redirect(unsigned int _dir, char* _path = nullptr); - - // 筛选错误等级 - static void restrict(unsigned int levels); - - static void close(); - -private: - static unsigned int dir; // 输出目标 - static unsigned int levels; // 错误等级 - static std::ofstream fs; // 输出文件流 -}; - -typedef Loghelper::Level Loglevel; - -#ifdef LOGHELPER_IMPLEMENT - -#define hasbit(flag, bit) ((flag & bit) == bit) - -unsigned int Loghelper::dir = Loghelper::Direction::DIR_CERR; -unsigned int Loghelper::levels = Loghelper::Level::LV_ALL; -std::ofstream Loghelper::fs; - -void Loghelper::log(Level _level, const char* _fmt, ...) -{ - if (!hasbit(levels, _level)) - return; -#define FORMAT_MSG_BUFFER_SIZE (204800) - const char* levelStr = nullptr; - switch (_level) - { - case LV_ERROR: - levelStr = "[Jin Error]:"; - break; - case LV_WARN: - levelStr = "[Jin Warn]:"; - break; - case LV_INFO: - levelStr = "[Jin Info]:"; - break; - case LV_DEBUG: - levelStr = "[Jin Debug]:"; - break; - default: - levelStr = "[Jin Unknown]:"; - break; - } - char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; - strcpy(buffer, levelStr); - va_list args; - va_start(args, _fmt); - vsnprintf(buffer + strlen(buffer), FORMAT_MSG_BUFFER_SIZE, _fmt, args); - va_end(args); - if (hasbit(dir, DIR_CERR)) - { - std::cerr << buffer << std::endl; - } - if (hasbit(dir, DIR_FILE)) - { - fs << buffer << std::endl; - } -#undef FORMAT_MSG_BUFFER_SIZE -} - -// 重定向 -void Loghelper::redirect(unsigned int _dir, char* _path) -{ - dir = _dir; - if (hasbit(dir, DIR_FILE)) - { - try - { - fs.open(_path, std::ios_base::app); - } - catch (std::ios_base::failure& e) { - dir = DIR_CERR; - log(Level::LV_WARN, "重定向log路径 %s 失败", _path); - } - } -} - -// 筛选错误等级 -void Loghelper::restrict(unsigned int _levels) -{ - levels = _levels; -} - -void Loghelper::close() -{ - if (!fs.fail()) - fs.close(); - fs.clear(); -} - -#undef hasbit - -#endif - -#endif diff --git a/src/libjin/Utils/macros.h b/src/libjin/Utils/macros.h deleted file mode 100644 index e19193c..0000000 --- a/src/libjin/Utils/macros.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __LIBJIN_MACROS_H -#define __LIBJIN_MACROS_H -#include - -//#define implement // 实现接口 -// -//#define shared // 声明类的方法 -// -//#define MASK // 声明掩码 enum -// -//#define onlyonce // 声明只会调用一次 -//#define CALLONCE(call) static char __dummy__=(call, 1) // 只会调用一次 -//#define SAFECALL(func, params) if(func) func(params) -// -//#define zero(mem) memset(&mem, 0, sizeof(mem)) - -#endif \ No newline at end of file diff --git a/src/libjin/Utils/unittest.cpp b/src/libjin/Utils/unittest.cpp deleted file mode 100644 index 44f1ae5..0000000 --- a/src/libjin/Utils/unittest.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "utils.h" -#if UNITTEST - -#include -#include -#include -#include "../audio/sdl/source.h" -#include "../audio/sdl/audio.h" - -using namespace jin::audio; -using namespace std; - -int main(int argc, char* argv[]) -{ - SDLAudio* audio = SDLAudio::get(); - audio->init(0); - SDLSource* source = SDLSource::createSource("a.ogg"); - SDLSource* source2 = SDLSource::createSource("a.wav"); - //source->play(); - source2->play(); - source->setLoop(true); - source2->setLoop(true); - int i = 0; - while (true) - { - SDL_Delay(1000); - } - audio->quit(); - return 0; -} - -/* -#include -#include -#include -#include "SDL2/SDL.h" - -#include <3rdparty/cmixer/cmixer.h> - -static SDL_mutex* audio_mutex; - -static void lock_handler(cm_Event *e) { - if (e->type == CM_EVENT_LOCK) { - SDL_LockMutex(audio_mutex); - } - if (e->type == CM_EVENT_UNLOCK) { - SDL_UnlockMutex(audio_mutex); - } -} - - -static void audio_callback(void *udata, Uint8 *stream, int size) { - cm_process((cm_Int16*)stream, size / 2); -} - - -int main(int argc, char **argv) { - SDL_AudioDeviceID dev; - SDL_AudioSpec fmt, got; - cm_Source *src; - cm_Source* src2; - - - SDL_Init(SDL_INIT_AUDIO); - audio_mutex = SDL_CreateMutex(); - - memset(&fmt, 0, sizeof(fmt)); - fmt.freq = 44100; - fmt.format = AUDIO_S16; - fmt.channels = 2; - fmt.samples = 1024; - fmt.callback = audio_callback; - - dev = SDL_OpenAudioDevice(NULL, 0, &fmt, &got, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); - if (dev == 0) { - fprintf(stderr, "Error: failed to open audio device '%s'\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - cm_init(got.freq); - cm_set_lock(lock_handler); - cm_set_master_gain(0.5); - - SDL_PauseAudioDevice(dev, 0); - - src = cm_new_source_from_file("a.ogg"); - src2 = cm_new_source_from_file("loop.wav"); - if (!src) { - fprintf(stderr, "Error: failed to create source '%s'\n", cm_get_error()); - exit(EXIT_FAILURE); - } - cm_set_loop(src2, 1); - - cm_play(src); - cm_play(src2); - - printf("Press [return] to exit\n"); - getchar(); - - cm_destroy_source(src); - SDL_CloseAudioDevice(dev); - SDL_Quit(); - - return EXIT_SUCCESS; -} -*/ - -#endif \ No newline at end of file diff --git a/src/libjin/Utils/utils.h b/src/libjin/Utils/utils.h deleted file mode 100644 index 1654a8f..0000000 --- a/src/libjin/Utils/utils.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef __LIBJIN_UTILS_H -#define __LIBJIN_UTILS_H - -#include "macros.h" -#include "endian.h" - -#define UNITTEST 0 - -#endif \ No newline at end of file -- cgit v1.1-26-g67d0