aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Utils')
-rw-r--r--src/libjin/Utils/Log.cpp2
-rw-r--r--src/libjin/Utils/Log.h134
-rw-r--r--src/libjin/Utils/endian.h23
-rw-r--r--src/libjin/Utils/macros.h16
-rw-r--r--src/libjin/Utils/unittest.cpp108
-rw-r--r--src/libjin/Utils/utils.h9
6 files changed, 292 insertions, 0 deletions
diff --git a/src/libjin/Utils/Log.cpp b/src/libjin/Utils/Log.cpp
new file mode 100644
index 0000000..5299942
--- /dev/null
+++ b/src/libjin/Utils/Log.cpp
@@ -0,0 +1,2 @@
+#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
new file mode 100644
index 0000000..b047402
--- /dev/null
+++ b/src/libjin/Utils/Log.h
@@ -0,0 +1,134 @@
+/**
+* Single.h/loghelper.h
+* Copyright (C) 2017~2018 chai
+*/
+#ifndef __LOG_HELPER_H
+#define __LOG_HELPER_H
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <stdarg.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_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)
+ char* levelStr = nullptr;
+ switch (_level)
+ {
+ case LV_ERROR:
+ levelStr = "Error: ";
+ break;
+ case LV_WARN:
+ levelStr = "Warn: ";
+ break;
+ case LV_INFO:
+ levelStr = "Info: ";
+ break;
+ case LV_DEBUG:
+ levelStr = "Debug: ";
+ break;
+ default:
+ levelStr = "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
new file mode 100644
index 0000000..d4c441a
--- /dev/null
+++ b/src/libjin/Utils/endian.h
@@ -0,0 +1,23 @@
+#ifndef JIN_LIL_ENDIAN && JIN_BIG_ENDIAN
+
+#define JIN_LIL_ENDIAN 2
+#define JIN_BIG_ENDIAN 4
+
+#endif
+
+#ifndef JIN_BYTEORDER
+#ifdef __linux__
+#include <endian.h>
+#define JIN_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 JIN_BYTEORDER JIN_BIG_ENDIAN
+#else
+#define JIN_BYTEORDER JIN_LIL_ENDIAN
+#endif
+#endif /* __linux__ */
+#endif /* !SDL_BYTEORDER */ \ No newline at end of file
diff --git a/src/libjin/Utils/macros.h b/src/libjin/Utils/macros.h
new file mode 100644
index 0000000..684e8e8
--- /dev/null
+++ b/src/libjin/Utils/macros.h
@@ -0,0 +1,16 @@
+#ifndef __JIN_MACROS_H
+#define __JIN_MACROS_H
+#include <cstring>
+
+#define implement // ʵֽӿ
+
+#define shared // ķ
+
+#define MASK // enum
+
+#define CALLONCE(call) static char __dummy__=(call, 1) // ֻһ
+#define onlyonce // ֻһ
+
+#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
new file mode 100644
index 0000000..2952b0b
--- /dev/null
+++ b/src/libjin/Utils/unittest.cpp
@@ -0,0 +1,108 @@
+#include "utils.h"
+#if UNITTEST
+
+#include <iostream>
+#include <stdio.h>
+#include <fstream>
+#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 <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/utils.h b/src/libjin/Utils/utils.h
new file mode 100644
index 0000000..cf0920e
--- /dev/null
+++ b/src/libjin/Utils/utils.h
@@ -0,0 +1,9 @@
+#ifndef __JIN_UTILS_H
+#define __JIN_UTILS_H
+
+#include "macros.h"
+#include "endian.h"
+
+#define UNITTEST 0
+
+#endif \ No newline at end of file