1
0
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:
jcorporation 2018-11-27 21:24:45 +00:00
parent cd7cf572f0
commit c1cc1ed852
4 changed files with 30 additions and 56 deletions

View File

@ -1,5 +1,10 @@
#myMPD config file #myMPD config file
#Loglevel
#1 = default
#2 = verbose
loglevel = 1
#Connection to mpd #Connection to mpd
mpdhost = 127.0.0.1 mpdhost = 127.0.0.1
mpdport = 6600 mpdport = 6600

View File

@ -64,9 +64,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
struct pollfd fds[1]; struct pollfd fds[1];
int pollrc; int pollrc;
#ifdef DEBUG printf("API request: %s\n", msg.p);
fprintf(stderr, "Got request: %s\n", msg.p);
#endif
je = json_scanf(msg.p, msg.len, "{cmd: %Q}", &cmd); je = json_scanf(msg.p, msg.len, "{cmd: %Q}", &cmd);
if (je == 1) if (je == 1)
@ -84,16 +82,11 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
pollrc = poll(fds, 1, 100); pollrc = poll(fds, 1, 100);
if (pollrc > 0) { if (pollrc > 0) {
idle_bitmask_save = mpd_recv_idle(mpd.conn, false); idle_bitmask_save = mpd_recv_idle(mpd.conn, false);
#ifdef DEBUG
if (idle_bitmask_save > 0) if (idle_bitmask_save > 0)
fprintf(stderr, "IDLE EVENT BEFORE REQUEST: %d\n", idle_bitmask_save); VERBOSELOG() fprintf(stderr, "IDLE EVENT BEFORE REQUEST: %d\n", idle_bitmask_save);
#endif
} }
mpd_response_finish(mpd.conn); mpd_response_finish(mpd.conn);
//handle request //handle request
#ifdef DEBUG
fprintf(stderr, "HANDLE REQUEST: %s\n", cmd);
#endif
switch(cmd_id) { switch(cmd_id) {
case MPD_API_UNKNOWN: case MPD_API_UNKNOWN:
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Unknown request\"}"); 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) { if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
#ifdef DEBUG printf("Error: %s\n", mpd_connection_get_error_message(mpd.conn));
fprintf(stderr, "Error: %s\n", mpd_connection_get_error_message(mpd.conn));
#endif
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}", n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
mpd_connection_get_error_message(mpd.conn)); 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); n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"No response for cmd %s\"}", cmd);
if (is_websocket(nc)) { if (is_websocket(nc)) {
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "Send websocket response:\n %s\n", mpd.buf);
fprintf(stderr, "Send websocket response:\n %s\n", mpd.buf);
#endif
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n); mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
} }
else { else {
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "Send http response (first 800 chars):\n%*.*s\n", 0, 800, mpd.buf);
fprintf(stderr, "Send http response (first 800 chars):\n%*.*s\n", 0, 800, mpd.buf);
#endif
mg_send_http_chunk(nc, mpd.buf, n); mg_send_http_chunk(nc, mpd.buf, n);
} }
free(cmd); free(cmd);
@ -682,9 +669,7 @@ void mympd_notify(struct mg_mgr *s) {
continue; continue;
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf)); mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
} }
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "NOTIFY: %s\n", mpd.buf);
fprintf(stderr, "NOTIFY: %s\n", mpd.buf);
#endif
} }
void mympd_parse_idle(struct mg_mgr *s, int idle_bitmask) { 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) if (idle_name == NULL)
break; break;
if (idle_bitmask & idle_event) { if (idle_bitmask & idle_event) {
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "IDLE: %s\n", idle_name);
fprintf(stderr, "IDLE: %s\n", idle_name);
#endif
switch(idle_event) { switch(idle_event) {
case MPD_IDLE_DATABASE: case MPD_IDLE_DATABASE:
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_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) { if (idle_bitmask_save > 0) {
//Handle idle event saved in mympd_callback //Handle idle event saved in mympd_callback
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "HANDLE SAVED IDLE EVENT\n");
fprintf(stderr, "HANDLE SAVED IDLE EVENT\n");
#endif
mympd_parse_idle(s, idle_bitmask_save); mympd_parse_idle(s, idle_bitmask_save);
idle_bitmask_save = 0; idle_bitmask_save = 0;
} }
@ -942,9 +923,7 @@ int mympd_get_updatedb_state(char *buffer) {
if (!status) if (!status)
RETURN_ERROR_AND_RECOVER("mpd_run_status"); RETURN_ERROR_AND_RECOVER("mpd_run_status");
update_id = mpd_status_get_update_id(status); update_id = mpd_status_get_update_id(status);
#ifdef DEBUG printf("Update database ID: %d\n", update_id);
fprintf(stderr, "Update database ID: %d\n", update_id);
#endif
if ( update_id > 0) if ( update_id > 0)
len = json_printf(&out, "{type: update_started, data: {jobid: %d}}", update_id); len = json_printf(&out, "{type: update_started, data: {jobid: %d}}", update_id);
else else
@ -1015,9 +994,7 @@ void mympd_count_song_uri(const char *uri, char *name, int value) {
else if (old_value < 0) else if (old_value < 0)
old_value = 0; old_value = 0;
snprintf(v, 4, "%d", old_value); snprintf(v, 4, "%d", old_value);
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "STICKER_COUNT_SONG: \"%s\" -> %s: %s\n", uri, name, v);
fprintf(stderr, "STICKER_COUNT_SONG: \"%s\" -> %s: %s\n", uri, name, v);
#endif
if (!mpd_run_sticker_set(mpd.conn, "song", uri, name, v)) if (!mpd_run_sticker_set(mpd.conn, "song", uri, name, v))
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set"); 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); read = getline(&line, &n, fp);
snprintf(value, 400, "%s", line); snprintf(value, 400, "%s", line);
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "State %s: %s\n", name, value);
printf("State %s: %s\n", name, value);
#endif
fclose(fp); fclose(fp);
if (read > 0) if (read > 0)
return true; return true;
@ -2604,10 +2579,6 @@ int mympd_smartpls_update(char *playlist, char *sticker, int maxentries) {
mpd_response_finish(mpd.conn); mpd_response_finish(mpd.conn);
free(uri); free(uri);
#ifdef DEBUG
printf("add_list length: %d\n", add_list.length);
#endif
mympd_smartpls_clear(playlist); mympd_smartpls_clear(playlist);
if (value_max > 2) if (value_max > 2)

View File

@ -65,6 +65,8 @@
} while (0) } while (0)
#define VERBOSELOG() if (config.loglevel == 2)
#define MAX_SIZE 2048 * 400 #define MAX_SIZE 2048 * 400
#define MAX_ELEMENTS_PER_PAGE 400 #define MAX_ELEMENTS_PER_PAGE 400
@ -204,6 +206,7 @@ typedef struct {
long streamport; long streamport;
const char *streamurl; const char *streamurl;
unsigned long last_played_count; unsigned long last_played_count;
long loglevel;
} t_config; } t_config;
t_config config; t_config config;

View File

@ -65,9 +65,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch(ev) { switch(ev) {
case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: { case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data; struct http_message *hm = (struct http_message *) ev_data;
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "New websocket request: %.*s\n", hm->uri.len, hm->uri.p);
fprintf(stderr, "New websocket request: %.*s\n", hm->uri.len, hm->uri.p);
#endif
if (mg_vcmp(&hm->uri, "/ws") != 0) { if (mg_vcmp(&hm->uri, "/ws") != 0) {
printf("Websocket request not to /ws, closing connection\n"); printf("Websocket request not to /ws, closing connection\n");
mg_printf(nc, "%s", "HTTP/1.1 403 FORBIDDEN\r\n\r\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; break;
} }
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "New Websocket connection established\n");
fprintf(stderr, "New Websocket connection established\n");
#endif
struct mg_str d = mg_mk_str("{\"cmd\": \"MPD_API_WELCOME\"}"); struct mg_str d = mg_mk_str("{\"cmd\": \"MPD_API_WELCOME\"}");
callback_mympd(nc, d); callback_mympd(nc, d);
break; break;
} }
case MG_EV_HTTP_REQUEST: { case MG_EV_HTTP_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data; struct http_message *hm = (struct http_message *) ev_data;
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "HTTP request: %.*s\n", hm->uri.len, hm->uri.p);
fprintf(stderr, "HTTP request: %.*s\n", hm->uri.len, hm->uri.p);
#endif
if (mg_vcmp(&hm->uri, "/api") == 0) if (mg_vcmp(&hm->uri, "/api") == 0)
handle_api(nc, hm); handle_api(nc, hm);
else else
@ -96,14 +90,10 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
} }
case MG_EV_CLOSE: { case MG_EV_CLOSE: {
if (is_websocket(nc)) { if (is_websocket(nc)) {
#ifdef DEBUG VERBOSELOG() fprintf(stderr, "Websocket connection closed\n");
fprintf(stderr, "Websocket connection closed\n");
#endif
} }
else { else {
#ifdef DEBUG VERBOSELOG() fprintf(stderr,"HTTP connection closed\n");
fprintf(stderr,"HTTP connection closed\n");
#endif
} }
break; break;
} }
@ -202,8 +192,12 @@ static int inihandler(void* user, const char* section, const char* name, const c
p_config->streamurl = strdup(value); p_config->streamurl = strdup(value);
else if (MATCH("last_played_count")) else if (MATCH("last_played_count"))
p_config->last_played_count = strtol(value, &crap, 10); 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 0; /* unknown section/name, error */
}
return 1; return 1;
} }
@ -403,6 +397,7 @@ int main(int argc, char **argv) {
config.etcdir = dirname(etcdir); config.etcdir = dirname(etcdir);
config.syscmds = false; config.syscmds = false;
config.localplayer = true; config.localplayer = true;
config.loglevel = 1;
mpd.timeout = 3000; mpd.timeout = 3000;
mpd.last_update_sticker_song_id = -1; mpd.last_update_sticker_song_id = -1;