mirror of
https://github.com/SuperBFG7/ympd
synced 2024-12-25 10:30:26 +00:00
Feat: add logging configuration option
This commit is contained in:
parent
cd7cf572f0
commit
c1cc1ed852
@ -1,5 +1,10 @@
|
||||
#myMPD config file
|
||||
|
||||
#Loglevel
|
||||
#1 = default
|
||||
#2 = verbose
|
||||
loglevel = 1
|
||||
|
||||
#Connection to mpd
|
||||
mpdhost = 127.0.0.1
|
||||
mpdport = 6600
|
||||
|
@ -64,9 +64,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
||||
struct pollfd fds[1];
|
||||
int pollrc;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Got request: %s\n", msg.p);
|
||||
#endif
|
||||
printf("API request: %s\n", msg.p);
|
||||
|
||||
je = json_scanf(msg.p, msg.len, "{cmd: %Q}", &cmd);
|
||||
if (je == 1)
|
||||
@ -84,16 +82,11 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
||||
pollrc = poll(fds, 1, 100);
|
||||
if (pollrc > 0) {
|
||||
idle_bitmask_save = mpd_recv_idle(mpd.conn, false);
|
||||
#ifdef DEBUG
|
||||
if (idle_bitmask_save > 0)
|
||||
fprintf(stderr, "IDLE EVENT BEFORE REQUEST: %d\n", idle_bitmask_save);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "IDLE EVENT BEFORE REQUEST: %d\n", idle_bitmask_save);
|
||||
}
|
||||
mpd_response_finish(mpd.conn);
|
||||
//handle request
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "HANDLE REQUEST: %s\n", cmd);
|
||||
#endif
|
||||
switch(cmd_id) {
|
||||
case MPD_API_UNKNOWN:
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Unknown request\"}");
|
||||
@ -645,9 +638,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
||||
}
|
||||
|
||||
if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Error: %s\n", mpd_connection_get_error_message(mpd.conn));
|
||||
#endif
|
||||
printf("Error: %s\n", mpd_connection_get_error_message(mpd.conn));
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
|
||||
mpd_connection_get_error_message(mpd.conn));
|
||||
|
||||
@ -662,15 +653,11 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"No response for cmd %s\"}", cmd);
|
||||
|
||||
if (is_websocket(nc)) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Send websocket response:\n %s\n", mpd.buf);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "Send websocket response:\n %s\n", mpd.buf);
|
||||
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
}
|
||||
else {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Send http response (first 800 chars):\n%*.*s\n", 0, 800, mpd.buf);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "Send http response (first 800 chars):\n%*.*s\n", 0, 800, mpd.buf);
|
||||
mg_send_http_chunk(nc, mpd.buf, n);
|
||||
}
|
||||
free(cmd);
|
||||
@ -682,9 +669,7 @@ void mympd_notify(struct mg_mgr *s) {
|
||||
continue;
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "NOTIFY: %s\n", mpd.buf);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "NOTIFY: %s\n", mpd.buf);
|
||||
}
|
||||
|
||||
void mympd_parse_idle(struct mg_mgr *s, int idle_bitmask) {
|
||||
@ -695,9 +680,7 @@ void mympd_parse_idle(struct mg_mgr *s, int idle_bitmask) {
|
||||
if (idle_name == NULL)
|
||||
break;
|
||||
if (idle_bitmask & idle_event) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "IDLE: %s\n", idle_name);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "IDLE: %s\n", idle_name);
|
||||
switch(idle_event) {
|
||||
case MPD_IDLE_DATABASE:
|
||||
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_database\"}");
|
||||
@ -921,9 +904,7 @@ void mympd_idle(struct mg_mgr *s, int timeout) {
|
||||
}
|
||||
if (idle_bitmask_save > 0) {
|
||||
//Handle idle event saved in mympd_callback
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "HANDLE SAVED IDLE EVENT\n");
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "HANDLE SAVED IDLE EVENT\n");
|
||||
mympd_parse_idle(s, idle_bitmask_save);
|
||||
idle_bitmask_save = 0;
|
||||
}
|
||||
@ -942,9 +923,7 @@ int mympd_get_updatedb_state(char *buffer) {
|
||||
if (!status)
|
||||
RETURN_ERROR_AND_RECOVER("mpd_run_status");
|
||||
update_id = mpd_status_get_update_id(status);
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Update database ID: %d\n", update_id);
|
||||
#endif
|
||||
printf("Update database ID: %d\n", update_id);
|
||||
if ( update_id > 0)
|
||||
len = json_printf(&out, "{type: update_started, data: {jobid: %d}}", update_id);
|
||||
else
|
||||
@ -1015,9 +994,7 @@ void mympd_count_song_uri(const char *uri, char *name, int value) {
|
||||
else if (old_value < 0)
|
||||
old_value = 0;
|
||||
snprintf(v, 4, "%d", old_value);
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "STICKER_COUNT_SONG: \"%s\" -> %s: %s\n", uri, name, v);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "STICKER_COUNT_SONG: \"%s\" -> %s: %s\n", uri, name, v);
|
||||
if (!mpd_run_sticker_set(mpd.conn, "song", uri, name, v))
|
||||
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
||||
}
|
||||
@ -1366,9 +1343,7 @@ bool mympd_state_get(char *name, char *value) {
|
||||
}
|
||||
read = getline(&line, &n, fp);
|
||||
snprintf(value, 400, "%s", line);
|
||||
#ifdef DEBUG
|
||||
printf("State %s: %s\n", name, value);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "State %s: %s\n", name, value);
|
||||
fclose(fp);
|
||||
if (read > 0)
|
||||
return true;
|
||||
@ -2604,10 +2579,6 @@ int mympd_smartpls_update(char *playlist, char *sticker, int maxentries) {
|
||||
mpd_response_finish(mpd.conn);
|
||||
free(uri);
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("add_list length: %d\n", add_list.length);
|
||||
#endif
|
||||
|
||||
mympd_smartpls_clear(playlist);
|
||||
|
||||
if (value_max > 2)
|
||||
|
@ -65,6 +65,8 @@
|
||||
} while (0)
|
||||
|
||||
|
||||
#define VERBOSELOG() if (config.loglevel == 2)
|
||||
|
||||
#define MAX_SIZE 2048 * 400
|
||||
#define MAX_ELEMENTS_PER_PAGE 400
|
||||
|
||||
@ -204,6 +206,7 @@ typedef struct {
|
||||
long streamport;
|
||||
const char *streamurl;
|
||||
unsigned long last_played_count;
|
||||
long loglevel;
|
||||
} t_config;
|
||||
|
||||
t_config config;
|
||||
|
27
src/mympd.c
27
src/mympd.c
@ -65,9 +65,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
switch(ev) {
|
||||
case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: {
|
||||
struct http_message *hm = (struct http_message *) ev_data;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "New websocket request: %.*s\n", hm->uri.len, hm->uri.p);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "New websocket request: %.*s\n", hm->uri.len, hm->uri.p);
|
||||
if (mg_vcmp(&hm->uri, "/ws") != 0) {
|
||||
printf("Websocket request not to /ws, closing connection\n");
|
||||
mg_printf(nc, "%s", "HTTP/1.1 403 FORBIDDEN\r\n\r\n");
|
||||
@ -76,18 +74,14 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
break;
|
||||
}
|
||||
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "New Websocket connection established\n");
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "New Websocket connection established\n");
|
||||
struct mg_str d = mg_mk_str("{\"cmd\": \"MPD_API_WELCOME\"}");
|
||||
callback_mympd(nc, d);
|
||||
break;
|
||||
}
|
||||
case MG_EV_HTTP_REQUEST: {
|
||||
struct http_message *hm = (struct http_message *) ev_data;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "HTTP request: %.*s\n", hm->uri.len, hm->uri.p);
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "HTTP request: %.*s\n", hm->uri.len, hm->uri.p);
|
||||
if (mg_vcmp(&hm->uri, "/api") == 0)
|
||||
handle_api(nc, hm);
|
||||
else
|
||||
@ -96,14 +90,10 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
}
|
||||
case MG_EV_CLOSE: {
|
||||
if (is_websocket(nc)) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Websocket connection closed\n");
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr, "Websocket connection closed\n");
|
||||
}
|
||||
else {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr,"HTTP connection closed\n");
|
||||
#endif
|
||||
VERBOSELOG() fprintf(stderr,"HTTP connection closed\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -202,8 +192,12 @@ static int inihandler(void* user, const char* section, const char* name, const c
|
||||
p_config->streamurl = strdup(value);
|
||||
else if (MATCH("last_played_count"))
|
||||
p_config->last_played_count = strtol(value, &crap, 10);
|
||||
else
|
||||
else if (MATCH("loglevel"))
|
||||
p_config->loglevel = strtol(value, &crap, 10);
|
||||
else {
|
||||
printf("Unkown config line: %s\n", name);
|
||||
return 0; /* unknown section/name, error */
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -403,6 +397,7 @@ int main(int argc, char **argv) {
|
||||
config.etcdir = dirname(etcdir);
|
||||
config.syscmds = false;
|
||||
config.localplayer = true;
|
||||
config.loglevel = 1;
|
||||
|
||||
mpd.timeout = 3000;
|
||||
mpd.last_update_sticker_song_id = -1;
|
||||
|
Loading…
Reference in New Issue
Block a user