diff options
Diffstat (limited to 'src/libjin/utils')
-rw-r--r-- | src/libjin/utils/je_endian.h | 23 | ||||
-rw-r--r-- | src/libjin/utils/je_log.cpp | 81 | ||||
-rw-r--r-- | src/libjin/utils/je_log.h | 63 | ||||
-rw-r--r-- | src/libjin/utils/je_macros.h | 17 | ||||
-rw-r--r-- | src/libjin/utils/je_unittest.cpp | 108 | ||||
-rw-r--r-- | src/libjin/utils/je_utils.h | 9 |
6 files changed, 301 insertions, 0 deletions
diff --git a/src/libjin/utils/je_endian.h b/src/libjin/utils/je_endian.h new file mode 100644 index 0000000..db8c8fd --- /dev/null +++ b/src/libjin/utils/je_endian.h @@ -0,0 +1,23 @@ +#ifndef jin_endian_lil && jin_endian_big + +#define jin_endian_lil 2 +#define jin_endian_big 4 + +#endif + +#ifndef jin_byte_order +#ifdef __linux__ +#include <endian.h> +#define jin_byte_order __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 jin_byte_order jin_endian_big +#else +#define jin_byte_order jin_endian_lil +#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..e369e14 --- /dev/null +++ b/src/libjin/utils/je_log.cpp @@ -0,0 +1,81 @@ +#define LOGHELPER_IMPLEMENT +#include "je_log.h" + +#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_WARNING: + levelStr = "[Jin Warning]: "; + break; + case LV_INFO: + levelStr = "[Jin Info]: "; + break; + case LV_DEBUG: + levelStr = "[Jin Debug]: "; + break; + default: + levelStr = "[Jin Unknow]: "; + 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_WARNING, "ضlog· %s ʧ", _path); + } + } +} + +// ɸѡȼ +void Loghelper::restrict(unsigned int _levels) +{ + levels = _levels; +} + +void Loghelper::close() +{ + if (!fs.fail()) + fs.close(); + fs.clear(); +} + +#undef hasbit diff --git a/src/libjin/utils/je_log.h b/src/libjin/utils/je_log.h new file mode 100644 index 0000000..f81bbae --- /dev/null +++ b/src/libjin/utils/je_log.h @@ -0,0 +1,63 @@ +#ifndef __LOG_H__ELPER_H__ +#define __LOG_H__ELPER_H__ + +#include <string> +#include <iostream> +#include <fstream> +#include <stdarg.h> + +#include "../core/je_configuration.h" + +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_WARNING = 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; + +#if defined(jin_debug) + #define jin_log_error(f, ...) Loghelper::log(Loghelper::LV_ERROR, f, __VA_ARGS__) + #define jin_log_info(f, ...) Loghelper::log(Loghelper::LV_INFO, f, __VA_ARGS__) + #define jin_log_warning(f, ...) Loghelper::log(Loghelper::LV_WARNING, f, __VA_ARGS__) + #define jin_log_debug(f, ...) Loghelper::log(Loghelper::LV_DEBUG, f, __VA_ARGS__) +#else + #define jin_log_error(f, ...) + #define jin_log_info(f, ...) + #define jin_log_warning(f, ...) + #define jin_log_debug(f, ...) +#endif + +#endif // __LOG_H__ELPER_H__
\ No newline at end of file diff --git a/src/libjin/utils/je_macros.h b/src/libjin/utils/je_macros.h new file mode 100644 index 0000000..6e4e7a9 --- /dev/null +++ b/src/libjin/utils/je_macros.h @@ -0,0 +1,17 @@ +#ifndef __JE_MACROS_H__ +#define __JE_MACROS_H__ +#include <cstring> + +//#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..294351b --- /dev/null +++ b/src/libjin/utils/je_unittest.cpp @@ -0,0 +1,108 @@ +#include "je_utils.h" +#if UNITTEST + +#include <iostream> +#include <stdio.h> +#include <fstream> +#include "../audio/sdl/source.h" +#include "../audio/sdl/audio.h" + +using namespace JinEngine::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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#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..a77c126 --- /dev/null +++ b/src/libjin/utils/je_utils.h @@ -0,0 +1,9 @@ +#ifndef __JE_UTILS_H__ +#define __JE_UTILS_H__ + +#include "je_macros.h" +#include "je_endian.h" + +#define UNITTEST 0 + +#endif
\ No newline at end of file |