mirror of
https://github.com/onyx-and-iris/vmrcli.git
synced 2024-11-15 17:40:56 +00:00
add logging
This commit is contained in:
parent
7d33496c1d
commit
7edb4df30e
58
include/log.h
Normal file
58
include/log.h
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2020 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See `log.c` for details.
|
||||
*/
|
||||
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#define LOG_VERSION "0.1.0"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
va_list ap;
|
||||
const char *fmt;
|
||||
const char *file;
|
||||
struct tm *time;
|
||||
void *udata;
|
||||
int line;
|
||||
int level;
|
||||
} log_Event;
|
||||
|
||||
typedef void (*log_LogFn)(log_Event *ev);
|
||||
typedef void (*log_LockFn)(bool lock, void *udata);
|
||||
|
||||
enum
|
||||
{
|
||||
LOG_TRACE,
|
||||
LOG_DEBUG,
|
||||
LOG_INFO,
|
||||
LOG_WARN,
|
||||
LOG_ERROR,
|
||||
LOG_FATAL
|
||||
};
|
||||
|
||||
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
|
||||
|
||||
const char *log_level_string(int level);
|
||||
void log_set_lock(log_LockFn fn, void *udata);
|
||||
void log_set_level(int level);
|
||||
void log_set_quiet(bool enable);
|
||||
int log_add_callback(log_LogFn fn, void *udata, int level);
|
||||
int log_add_fp(FILE *fp, int level);
|
||||
|
||||
void log_log(int level, const char *file, int line, const char *fmt, ...);
|
||||
|
||||
#endif
|
179
src/log.c
Normal file
179
src/log.c
Normal file
@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright (c) 2020 rxi
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#define MAX_CALLBACKS 32
|
||||
|
||||
typedef struct
|
||||
{
|
||||
log_LogFn fn;
|
||||
void *udata;
|
||||
int level;
|
||||
} Callback;
|
||||
|
||||
static struct
|
||||
{
|
||||
void *udata;
|
||||
log_LockFn lock;
|
||||
int level;
|
||||
bool quiet;
|
||||
Callback callbacks[MAX_CALLBACKS];
|
||||
} L;
|
||||
|
||||
static const char *level_strings[] = {
|
||||
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
|
||||
|
||||
#ifdef LOG_USE_COLOR
|
||||
static const char *level_colors[] = {
|
||||
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"};
|
||||
#endif
|
||||
|
||||
static void stdout_callback(log_Event *ev)
|
||||
{
|
||||
char buf[16];
|
||||
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
||||
#ifdef LOG_USE_COLOR
|
||||
fprintf(
|
||||
ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
|
||||
buf, level_colors[ev->level], level_strings[ev->level],
|
||||
ev->file, ev->line);
|
||||
#else
|
||||
fprintf(
|
||||
ev->udata, "%s %-5s %s:%d: ",
|
||||
buf, level_strings[ev->level], ev->file, ev->line);
|
||||
#endif
|
||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||
fprintf(ev->udata, "\n");
|
||||
fflush(ev->udata);
|
||||
}
|
||||
|
||||
static void file_callback(log_Event *ev)
|
||||
{
|
||||
char buf[64];
|
||||
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
||||
fprintf(
|
||||
ev->udata, "%s %-5s %s:%d: ",
|
||||
buf, level_strings[ev->level], ev->file, ev->line);
|
||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||
fprintf(ev->udata, "\n");
|
||||
fflush(ev->udata);
|
||||
}
|
||||
|
||||
static void lock(void)
|
||||
{
|
||||
if (L.lock)
|
||||
{
|
||||
L.lock(true, L.udata);
|
||||
}
|
||||
}
|
||||
|
||||
static void unlock(void)
|
||||
{
|
||||
if (L.lock)
|
||||
{
|
||||
L.lock(false, L.udata);
|
||||
}
|
||||
}
|
||||
|
||||
const char *log_level_string(int level)
|
||||
{
|
||||
return level_strings[level];
|
||||
}
|
||||
|
||||
void log_set_lock(log_LockFn fn, void *udata)
|
||||
{
|
||||
L.lock = fn;
|
||||
L.udata = udata;
|
||||
}
|
||||
|
||||
void log_set_level(int level)
|
||||
{
|
||||
L.level = level;
|
||||
}
|
||||
|
||||
void log_set_quiet(bool enable)
|
||||
{
|
||||
L.quiet = enable;
|
||||
}
|
||||
|
||||
int log_add_callback(log_LogFn fn, void *udata, int level)
|
||||
{
|
||||
for (int i = 0; i < MAX_CALLBACKS; i++)
|
||||
{
|
||||
if (!L.callbacks[i].fn)
|
||||
{
|
||||
L.callbacks[i] = (Callback){fn, udata, level};
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int log_add_fp(FILE *fp, int level)
|
||||
{
|
||||
return log_add_callback(file_callback, fp, level);
|
||||
}
|
||||
|
||||
static void init_event(log_Event *ev, void *udata)
|
||||
{
|
||||
if (!ev->time)
|
||||
{
|
||||
time_t t = time(NULL);
|
||||
ev->time = localtime(&t);
|
||||
}
|
||||
ev->udata = udata;
|
||||
}
|
||||
|
||||
void log_log(int level, const char *file, int line, const char *fmt, ...)
|
||||
{
|
||||
log_Event ev = {
|
||||
.fmt = fmt,
|
||||
.file = file,
|
||||
.line = line,
|
||||
.level = level,
|
||||
};
|
||||
|
||||
lock();
|
||||
|
||||
if (!L.quiet && level >= L.level)
|
||||
{
|
||||
init_event(&ev, stderr);
|
||||
va_start(ev.ap, fmt);
|
||||
stdout_callback(&ev);
|
||||
va_end(ev.ap);
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++)
|
||||
{
|
||||
Callback *cb = &L.callbacks[i];
|
||||
if (level >= cb->level)
|
||||
{
|
||||
init_event(&ev, cb->udata);
|
||||
va_start(ev.ap, fmt);
|
||||
cb->fn(&ev);
|
||||
va_end(ev.ap);
|
||||
}
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
18
src/vmr.c
18
src/vmr.c
@ -1,6 +1,7 @@
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include "vmr.h"
|
||||
#include "log.h"
|
||||
|
||||
long login(T_VBVMR_INTERFACE *iVMR, int kind)
|
||||
{
|
||||
@ -14,16 +15,16 @@ long login(T_VBVMR_INTERFACE *iVMR, int kind)
|
||||
switch (kind)
|
||||
{
|
||||
case BASIC:
|
||||
puts("Launching Voicemeeter Basic GUI");
|
||||
log_info("Launching Voicemeeter Basic GUI");
|
||||
break;
|
||||
case BANANA:
|
||||
puts("Launching Voicemeeter Banana GUI");
|
||||
log_info("Launching Voicemeeter Banana GUI");
|
||||
break;
|
||||
case POTATO:
|
||||
puts("Launching Voicemeeter Potato GUI");
|
||||
log_info("Launching Voicemeeter Potato GUI");
|
||||
break;
|
||||
case POTATOX64:
|
||||
puts("Launching Voicemeeter Potato x64 GUI");
|
||||
log_info("Launching Voicemeeter Potato x64 GUI");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -31,7 +32,7 @@ long login(T_VBVMR_INTERFACE *iVMR, int kind)
|
||||
}
|
||||
if (rep == 0)
|
||||
{
|
||||
puts("Successfully logged into the Voicemeeter API");
|
||||
log_info("Successfully logged into the Voicemeeter API");
|
||||
clear_dirty(iVMR);
|
||||
}
|
||||
return rep;
|
||||
@ -39,8 +40,13 @@ long login(T_VBVMR_INTERFACE *iVMR, int kind)
|
||||
|
||||
long logout(T_VBVMR_INTERFACE *iVMR)
|
||||
{
|
||||
int rep;
|
||||
|
||||
Sleep(20); /* give time for last command */
|
||||
return iVMR->VBVMR_Logout();
|
||||
rep = iVMR->VBVMR_Logout();
|
||||
if (rep == 0)
|
||||
log_info("Successfully logged out of the Voicemeeter API");
|
||||
return rep;
|
||||
}
|
||||
|
||||
long run_voicemeeter(T_VBVMR_INTERFACE *iVMR, int kind)
|
||||
|
33
src/vmrcli.c
33
src/vmrcli.c
@ -4,6 +4,7 @@
|
||||
#include <getopt.h>
|
||||
#include "cdll.h"
|
||||
#include "vmr.h"
|
||||
#include "log.h"
|
||||
|
||||
#define MAX_LINE 512
|
||||
|
||||
@ -35,9 +36,12 @@ int main(int argc, char *argv[])
|
||||
bool iflag = false;
|
||||
int opt;
|
||||
char *kvalue = "";
|
||||
int dvalue;
|
||||
int kind = BANANA;
|
||||
|
||||
while ((opt = getopt(argc, argv, "k:ih")) != -1)
|
||||
log_set_level(LOG_INFO);
|
||||
|
||||
while ((opt = getopt(argc, argv, "k:ihD:")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
@ -46,10 +50,17 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
case 'k':
|
||||
kvalue = optarg;
|
||||
kind = set_kind(kvalue);
|
||||
break;
|
||||
case 'h':
|
||||
help();
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'D':
|
||||
if ((dvalue = atoi(optarg)) && dvalue >= LOG_TRACE && dvalue <= LOG_FATAL)
|
||||
{
|
||||
log_set_level(dvalue);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
@ -57,10 +68,6 @@ int main(int argc, char *argv[])
|
||||
|
||||
T_VBVMR_INTERFACE iVMR;
|
||||
T_VBVMR_INTERFACE *vmr = &iVMR;
|
||||
if (kvalue && kvalue[0] != '\0')
|
||||
{
|
||||
kind = set_kind(kvalue);
|
||||
}
|
||||
|
||||
int rep = init_voicemeeter(vmr, kind);
|
||||
if (rep != 0)
|
||||
@ -83,14 +90,9 @@ int main(int argc, char *argv[])
|
||||
|
||||
rep = logout(vmr);
|
||||
if (rep == 0)
|
||||
{
|
||||
puts("Successfully logged out of the Voicemeeter API");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
void help()
|
||||
@ -99,7 +101,8 @@ void help()
|
||||
"Usage: ./vmrcli.exe [-i] [-k] <api commands>\n"
|
||||
"Where: \n"
|
||||
"\ti: Enable interactive mode\n"
|
||||
"\tk: The kind of Voicemeeter (basic, banana, potato)");
|
||||
"\tk: The kind of Voicemeeter (basic, banana, potato)\n"
|
||||
"\tD: Set Debug level 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL");
|
||||
}
|
||||
|
||||
int set_kind(char *kval)
|
||||
@ -159,7 +162,8 @@ void interactive(T_VBVMR_INTERFACE *vmr)
|
||||
|
||||
while (fgets(input, MAX_LINE, stdin) != NULL)
|
||||
{
|
||||
if (strlen(input) == 2 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0))
|
||||
input[strcspn(input, "\n")] = 0;
|
||||
if (strlen(input) == 1 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0))
|
||||
break;
|
||||
|
||||
while (*p)
|
||||
@ -167,7 +171,7 @@ void interactive(T_VBVMR_INTERFACE *vmr)
|
||||
char command[MAX_LINE];
|
||||
int i = 0;
|
||||
|
||||
while (!isspace(*p))
|
||||
while (!isspace(*p) && *p != EOF)
|
||||
command[i++] = *p++;
|
||||
command[i] = '\0';
|
||||
p++; /* shift to next char */
|
||||
@ -181,7 +185,8 @@ void interactive(T_VBVMR_INTERFACE *vmr)
|
||||
|
||||
void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
|
||||
{
|
||||
printf("Parsing %s\n", command);
|
||||
log_debug("Parsing %s", command);
|
||||
|
||||
if (command[0] == '!') /* toggle */
|
||||
{
|
||||
command++;
|
||||
|
Loading…
Reference in New Issue
Block a user