Skip to content

Commit

Permalink
Source code coverage with debugging routines
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed May 8, 2014
1 parent b2025d7 commit f324c08
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 36 deletions.
6 changes: 6 additions & 0 deletions cmusfm.debug
@@ -0,0 +1,6 @@
#!/bin/sh

date +%s >>/tmp/cmusfm.debug
echo "--- $@" >>/tmp/cmusfm.debug
cmusfm "$@" >>/tmp/cmusfm.debug 2>&1
echo "---" >>/tmp/cmusfm.debug
13 changes: 9 additions & 4 deletions src/cache.c
Expand Up @@ -24,6 +24,7 @@
#include <fcntl.h>
#include "cmusfm.h"
#include "cache.h"
#include "debug.h"


// Write data to cache file which should be submitted later.
Expand All @@ -35,6 +36,11 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf)
int fd, data_size;
int artlen, tralen, alblen;

debug("cache update: %ld", sb_tinf->timestamp);
debug("payload: %s - %s (%s) - %d. %s (%ds)",
sb_tinf->artist, sb_tinf->album, sb_tinf->album_artist,
sb_tinf->track_number, sb_tinf->track, sb_tinf->duration);

fd = open(get_cmusfm_cache_file(), O_CREAT | O_APPEND | O_WRONLY, 00644);
if(fd == -1) return;

Expand Down Expand Up @@ -65,6 +71,8 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs)
void *record;
int rec_size;

debug("cache submit");

fname = get_cmusfm_cache_file();
if((fd = open(fname, O_RDONLY)) == -1)
return; // no cache file -> nothing to submit :)
Expand All @@ -75,6 +83,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs)

for(record = rd_buff;;) {
rec_size = *((int*)record);
debug("record size: %d", rec_size);

// break if current record is truncated
if(record - (void*)rd_buff + rec_size > rd_len) break;
Expand All @@ -85,11 +94,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs)
sb_tinf.album = &sb_tinf.track[strlen(sb_tinf.track) + 1];

// submit tracks to Last.fm
#ifndef DEBUG
scrobbler_scrobble(sbs, &sb_tinf);
#else
dump_trackinfo("cmusfm_cache_submit", &sb_tinf);
#endif

// point to next record
record += rec_size;
Expand Down
1 change: 0 additions & 1 deletion src/cmusfm.h
Expand Up @@ -73,6 +73,5 @@ struct format_match {
char *get_cmus_home_dir();
struct format_match *get_regexp_format_matches(const char *str, const char *format);
struct format_match *get_regexp_match(struct format_match *matches, enum format_match_type type);
void dump_trackinfo(const char *info, const scrobbler_trackinfo_t *sb_tinf);

#endif
32 changes: 32 additions & 0 deletions src/debug.h
@@ -0,0 +1,32 @@
/*
* cmusfm - debug.h
* Copyright (c) 2014 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
* cmusfm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cmusfm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* If you want to read full version of the GNU General Public License
* see <http://www.gnu.org/licenses/>.
*/

#ifndef __DEBUG_H
#define __DEBUG_H

#include <stdio.h>

#ifdef DEBUG
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define debug(M, ...)
#endif

#endif
20 changes: 20 additions & 0 deletions src/libscrobbler2.c
Expand Up @@ -25,6 +25,7 @@
#include <curl/curl.h>
#include <openssl/md5.h>
#include "libscrobbler2.h"
#include "debug.h"


// used as a buffer for GET/POST server response
Expand All @@ -51,6 +52,7 @@ static size_t sb_curl_write_callback(char *ptr, size_t size, size_t nmemb,
struct sb_response_data *resp = (struct sb_response_data*)data;
int len = size * nmemb;

debug("read: len: %d, body: %s", len, ptr);
resp->data = realloc(resp->data, resp->len + len + 1);
memcpy(&resp->data[resp->len], ptr, len);
resp->len += len;
Expand Down Expand Up @@ -94,6 +96,7 @@ int sb_check_response(struct sb_response_data *response, int curl_status,
{
char *ptr;

debug("check: status: %d, body: %s", curl_status, response->data);
if(curl_status != 0) {
// network transfer failure (curl error)
sbs->error_code = curl_status;
Expand Down Expand Up @@ -130,6 +133,7 @@ void sb_generate_method_signature(struct sb_getpost_data *sb_data, int len,
offset += sprintf(tmp_str + offset, format, sb_data[x].name, sb_data[x].data);
}

debug("signature data: %s", tmp_str);
strcat(tmp_str, secret_hex);
MD5((unsigned char*)tmp_str, strlen(tmp_str), sign);
}
Expand Down Expand Up @@ -158,6 +162,7 @@ char *sb_make_curl_getpost_string(CURL *curl, char *str_buffer,
}

str_buffer[offset - 1] = 0; //strip '&' at the end of string
debug("params: %s", str_buffer);
return str_buffer;
}

Expand Down Expand Up @@ -190,6 +195,11 @@ int scrobbler_scrobble(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt)
//{"streamId", 's', NULL},
{"api_sig", 's', sign_hex}};

debug("scrobble: %ld", sbt->timestamp);
debug("payload: %s - %s (%s) - %d. %s (%ds)",
sbt->artist, sbt->album, sbt->album_artist,
sbt->track_number, sbt->track, sbt->duration);

if(sbt->artist == NULL || sbt->track == NULL || sbt->timestamp == 0)
return SCROBBERR_TRACKINF;

Expand All @@ -209,6 +219,7 @@ int scrobbler_scrobble(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt)
curl_easy_setopt(curl, CURLOPT_URL, SCROBBLER_URL);
status = curl_easy_perform(curl);
status = sb_check_response(&response, status, sbs);
debug("scrobble status: %d", status);

sb_curl_cleanup(curl, &response);
return status;
Expand Down Expand Up @@ -243,6 +254,11 @@ int sb_update_now_playing(scrobbler_session_t *sbs,
{"trackNumber", 'd', (void*)(long)sbt->track_number},
{"api_sig", 's', sign_hex}};

debug("now playing: %ld", sbt->timestamp);
debug("payload: %s - %s (%s) - %d. %s (%ds)",
sbt->artist, sbt->album, sbt->album_artist,
sbt->track_number, sbt->track, sbt->duration);

if((curl = sb_curl_init(CURLOPT_POST, &response)) == NULL)
return SCROBBERR_CURLINIT;

Expand All @@ -259,6 +275,7 @@ int sb_update_now_playing(scrobbler_session_t *sbs,
curl_easy_setopt(curl, CURLOPT_URL, SCROBBLER_URL);
status = curl_easy_perform(curl);
status = sb_check_response(&response, status, sbs);
debug("now playing status: %d", status);

sb_curl_cleanup(curl, &response);
return status;
Expand All @@ -268,6 +285,7 @@ int sb_update_now_playing(scrobbler_session_t *sbs,
int scrobbler_update_now_playing(scrobbler_session_t *sbs,
scrobbler_trackinfo_t *sbt)
{
debug("now playing wrapper");
if(sbt->artist == NULL || sbt->track == NULL)
return SCROBBERR_TRACKINF;
return sb_update_now_playing(sbs, sbt);
Expand All @@ -281,6 +299,7 @@ int scrobbler_test_session_key(scrobbler_session_t *sbs)
scrobbler_trackinfo_t sbt;
int status;

debug("test service connection");
memset(&sbt, 0, sizeof(sbt));
status = sb_update_now_playing(sbs, &sbt);

Expand Down Expand Up @@ -370,6 +389,7 @@ int scrobbler_authentication(scrobbler_session_t *sbs,
curl_easy_setopt(curl, CURLOPT_URL, get_url);
status = curl_easy_perform(curl);
status = sb_check_response(&response, status, sbs);
debug("authentication status: %d", status);

if(status != 0) {
sb_curl_cleanup(curl, &response);
Expand Down
22 changes: 6 additions & 16 deletions src/main.c
Expand Up @@ -25,6 +25,7 @@
#include "cmusfm.h"
#include "config.h"
#include "server.h"
#include "debug.h"


// Last.fm API key for cmusfm
Expand Down Expand Up @@ -64,6 +65,8 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form
regex_t regex;
regmatch_t regmatch[MATCHES_SIZE];

debug("matching: %s: %s", format, str);

// allocate memory for up to FORMAT_MATCH_TYPE_COUNT matches
// with one extra always empty terminating structure
matches = (struct format_match*)calloc(MATCHES_SIZE, sizeof(*matches));
Expand All @@ -75,6 +78,8 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form
strcpy(&regexp[p - format - i * 2], p);
}

debug("regexp: %s", regexp);

status = regcomp(&regex, regexp, REG_EXTENDED | REG_ICASE);
free(regexp);
if(status) {
Expand Down Expand Up @@ -106,25 +111,14 @@ struct format_match *get_regexp_match(struct format_match *matches, enum format_
return matches;
}

#ifdef DEBUG
void dump_trackinfo(const char *info, const scrobbler_trackinfo_t *sb_tinf)
{
printf("--= %s =--\n", info);
printf(" timestamp: %d duration: %ds MbID: %s\n",
(int)sb_tinf->timestamp, sb_tinf->duration, sb_tinf->mbid);
printf(" %s - %s (%s) - %02d. %s\n", sb_tinf->artist, sb_tinf->album,
sb_tinf->album_artist, sb_tinf->track_number, sb_tinf->track);
fflush(stdout);
}
#endif

// Parse arguments which we've get from the cmus.
int parse_argv(struct cmtrack_info *tinfo, int argc, char *argv[])
{
int i;

memset(tinfo, 0, sizeof(*tinfo));
for(i = 1; i + 1 < argc; i += 2) {
debug("cmus argv: %s %s", argv[i], argv[i + 1]);
if(strcmp(argv[i], "status") == 0) {
if(strcmp(argv[i + 1], "playing") == 0)
tinfo->status = CMSTATUS_PLAYING;
Expand All @@ -147,10 +141,6 @@ int parse_argv(struct cmtrack_info *tinfo, int argc, char *argv[])
tinfo->title = argv[i + 1];
else if(strcmp(argv[i], "duration") == 0)
tinfo->duration = atoi(argv[i + 1]);
#ifdef DEBUG
else
printf("unhandled argv: %s: %s\n", argv[i], argv[i + 1]);
#endif
}

// NOTE: cmus always passes status parameter
Expand Down
36 changes: 21 additions & 15 deletions src/server.c
Expand Up @@ -29,6 +29,7 @@
#include "cmusfm.h"
#include "server.h"
#include "cache.h"
#include "debug.h"


void set_trackinfo(scrobbler_trackinfo_t *sbt, const struct sock_data_tag *dt)
Expand Down Expand Up @@ -68,16 +69,15 @@ void cmusfm_server_process_data(int fd, scrobbler_session_t *sbs)
memset(&sb_tinf, 0, sizeof(sb_tinf));

rd_len = read(fd, buffer, sizeof(buffer));
debug("rdlen: %d, status: %d", rd_len, sock_data->status);

if(rd_len < sizeof(struct sock_data_tag))
return; // something was wrong...

#ifdef DEBUG
printf("sockrdlen: %d status: 0x%02x duration: %ds\n %s - %s - %02d. %s\n",
rd_len, sock_data->status, sock_data->duration,
debug("payload: %s - %s - %d. %s (%ds)",
(char *)(sock_data + 1), &((char *)(sock_data + 1))[sock_data->alboff],
sock_data->tracknb, &((char *)(sock_data + 1))[sock_data->titoff]);
fflush(stdout);
#endif
sock_data->tracknb, &((char *)(sock_data + 1))[sock_data->titoff],
sock_data->duration);

// make data hash without status field
new_hash = make_data_hash((unsigned char*)&buffer[1], rd_len - 1);
Expand Down Expand Up @@ -107,19 +107,17 @@ void cmusfm_server_process_data(int fd, scrobbler_session_t *sbs)
sb_tinf.timestamp = started;

if((saved_is_radio && !config.submit_shoutcast) ||
(!saved_is_radio && !config.submit_localfile))
(!saved_is_radio && !config.submit_localfile)) {
// skip submission if we don't want it
debug("submission not enabled");
goto submission_skip;
}

if(scrobbler_fail_time == 0) {
#ifndef DEBUG
if(scrobbler_scrobble(sbs, &sb_tinf) != 0) {
scrobbler_fail_time = 1;
goto submission_failed;
}
#else
dump_trackinfo("submission", &sb_tinf);
#endif
}
else // write data to cache
submission_failed:
Expand Down Expand Up @@ -149,13 +147,13 @@ void cmusfm_server_process_data(int fd, scrobbler_session_t *sbs)
if((saved_is_radio && config.nowplaying_shoutcast) ||
(!saved_is_radio && config.nowplaying_localfile)) {
set_trackinfo(&sb_tinf, sock_data);
#ifndef DEBUG
if(scrobbler_update_now_playing(sbs, &sb_tinf) != 0)
scrobbler_fail_time = 1;
#else
dump_trackinfo("update_now_playing", &sb_tinf);
#endif
}
#if DEBUG
else
debug("now playing not enabled");
#endif
}
}
}
Expand Down Expand Up @@ -198,6 +196,8 @@ void cmusfm_server_start()
fd_set rd_fds;
scrobbler_session_t *sbs;

debug("starting cmusfm server");

memset(&sock_a, 0, sizeof(sock_a));
sock_a.sun_family = AF_UNIX;
strcpy(sock_a.sun_path, get_cmusfm_socket_file());
Expand Down Expand Up @@ -225,6 +225,7 @@ void cmusfm_server_start()
sigaction(SIGINT, &sigact, NULL);

// create server communication socket (no error check)
unlink(sock_a.sun_path);
bind(sock, (struct sockaddr*)(&sock_a), sizeof(sock_a));
listen(sock, 2);

Expand Down Expand Up @@ -262,6 +263,8 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo)
struct sockaddr_un sock_a;
int sock;

debug("sending track to cmusfm server");

memset(buffer, 0, sizeof(buffer));

// load data into the sock container
Expand All @@ -278,6 +281,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo)
(tinfo->file != NULL && tinfo->artist == NULL && tinfo->title == NULL)) {
// NOTE: Automatic format detection mode.
// When title and artist was not specified but URL or file is available.
debug("regular expression matching mode");

if(tinfo->url != NULL) {
// URL: try to fetch artist and track tile form the 'title' field
Expand Down Expand Up @@ -335,6 +339,8 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo)
return -1;
}

debug("socket wrlen: %ld", sizeof(struct sock_data_tag) +
sock_data->titoff + strlen(title) + 1);
write(sock, buffer, sizeof(struct sock_data_tag) + sock_data->titoff +
strlen(title) + 1);
return close(sock);
Expand Down

0 comments on commit f324c08

Please sign in to comment.