diff --git a/include/Makefile.am b/include/Makefile.am index 81419f7..c20d3ca 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,4 +1,5 @@ noinst_HEADERS = \ + osmocom/gapk/logging.h \ osmocom/gapk/utils.h \ osmocom/gapk/bench.h \ $(NULL) @@ -9,6 +10,7 @@ nobase_include_HEADERS = \ osmocom/gapk/procqueue.h \ osmocom/gapk/formats.h \ osmocom/gapk/codecs.h \ + osmocom/gapk/common.h \ $(NULL) if ENABLE_GSMHR diff --git a/include/osmocom/gapk/common.h b/include/osmocom/gapk/common.h new file mode 100644 index 0000000..138882c --- /dev/null +++ b/include/osmocom/gapk/common.h @@ -0,0 +1,21 @@ +/* + * This file is part of gapk (GSM Audio Pocket Knife). + * + * gapk 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. + * + * gapk 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. + * + * You should have received a copy of the GNU General Public License + * along with gapk. If not, see . + * + */ + +#pragma once + +void osmo_gapk_log_init(int subsys); diff --git a/include/osmocom/gapk/logging.h b/include/osmocom/gapk/logging.h new file mode 100644 index 0000000..eaa82ab --- /dev/null +++ b/include/osmocom/gapk/logging.h @@ -0,0 +1,28 @@ +/* + * This file is part of gapk (GSM Audio Pocket Knife). + * + * gapk 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. + * + * gapk 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. + * + * You should have received a copy of the GNU General Public License + * along with gapk. If not, see . + * + */ + +#pragma once + +#include + +extern int osmo_gapk_log_init_complete; +extern int osmo_gapk_log_subsys; + +#define LOGPGAPK(level, fmt, args...) \ + if (osmo_gapk_log_init_complete) \ + LOGP(osmo_gapk_log_subsys, level, fmt, ## args) diff --git a/src/Makefile.am b/src/Makefile.am index 2c52ec1..060d435 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -76,6 +76,11 @@ libosmogapk_la_SOURCES += \ benchmark.c \ $(NULL) +# Logging +libosmogapk_la_SOURCES += \ + logging.c \ + $(NULL) + # libosmogapk representative application bin_PROGRAMS = osmo-gapk diff --git a/src/app_osmo_gapk.c b/src/app_osmo_gapk.c index 90779af..926f3ba 100644 --- a/src/app_osmo_gapk.c +++ b/src/app_osmo_gapk.c @@ -31,8 +31,12 @@ #include #include +#include +#include #include +#include +#include #include #include #include @@ -85,6 +89,25 @@ struct gapk_state } out; }; +/* Logging related routines */ +enum { + DAPP, +}; + +static struct log_info_cat gapk_log_info_cat[] = { + [DAPP] = { + .name = "DAPP", + .description = "Application", + .color = "\033[0;36m", + .enabled = 1, .loglevel = LOGL_DEBUG, + }, +}; + +static const struct log_info gapk_log_info = { + .cat = gapk_log_info_cat, + .num_cat = ARRAY_SIZE(gapk_log_info_cat), +}; + static void print_help(char *progname) @@ -620,6 +643,11 @@ int main(int argc, char *argv[]) { int rv; + /* Init Osmocom logging framework */ + osmo_init_logging(&gapk_log_info); + /* and GAPK logging wrapper */ + osmo_gapk_log_init(DAPP); + /* Clear state */ memset(gs, 0x00, sizeof(struct gapk_state)); gs->in.rtp.fd = -1; diff --git a/src/libosmogapk.map b/src/libosmogapk.map index 0ab0669..d8b8af3 100644 --- a/src/libosmogapk.map +++ b/src/libosmogapk.map @@ -1,6 +1,8 @@ LIBOSMOGAPK_1.0 { global: +osmo_gapk_log_init; + osmo_gapk_pq; osmo_gapk_pq_item; diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..96313ae --- /dev/null +++ b/src/logging.c @@ -0,0 +1,27 @@ +/* + * This file is part of gapk (GSM Audio Pocket Knife). + * + * (C) 2017 by Vadim Yanitskiy + * + * gapk 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. + * + * gapk 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. + * + * You should have received a copy of the GNU General Public License + * along with gapk. If not, see . + */ + +int osmo_gapk_log_init_complete = 0; +int osmo_gapk_log_subsys; + +void osmo_gapk_log_init(int subsys) +{ + osmo_gapk_log_subsys = subsys; + osmo_gapk_log_init_complete = 1; +} diff --git a/src/pq_alsa.c b/src/pq_alsa.c index d280de6..b91457e 100644 --- a/src/pq_alsa.c +++ b/src/pq_alsa.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -147,7 +148,7 @@ out_close: snd_pcm_close(state->pcm_handle); free(state); out_print: - fprintf(stderr, "[!] Couldn't init ALSA device '%s': %s\n", + LOGPGAPK(LOGL_ERROR, "Couldn't init ALSA device '%s': %s\n", alsa_dev, snd_strerror(rc)); return rc; } @@ -162,7 +163,8 @@ out_print: int osmo_gapk_pq_queue_alsa_input(struct osmo_gapk_pq *pq, const char *hwdev, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding ALSA input (dev='%s', blk_len=%u)\n", hwdev, blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding ALSA input " + "(dev='%s', blk_len=%u)\n", hwdev, blk_len); return pq_queue_alsa_op(pq, hwdev, blk_len, 1); } @@ -175,7 +177,8 @@ osmo_gapk_pq_queue_alsa_input(struct osmo_gapk_pq *pq, const char *hwdev, unsign int osmo_gapk_pq_queue_alsa_output(struct osmo_gapk_pq *pq, const char *hwdev, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding ALSA output (dev='%s', blk_len=%u)\n", hwdev, blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding ALSA output " + "(dev='%s', blk_len=%u)\n", hwdev, blk_len); return pq_queue_alsa_op(pq, hwdev, blk_len, 0); } diff --git a/src/pq_codec.c b/src/pq_codec.c index 37fa1c9..f5da628 100644 --- a/src/pq_codec.c +++ b/src/pq_codec.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -71,7 +72,7 @@ osmo_gapk_pq_queue_codec(struct osmo_gapk_pq *pq, const struct osmo_gapk_codec_d item->exit = codec->codec_exit; - fprintf(stderr, "[+] PQ: Adding Codec %s, %s format %s\n", codec->name, + LOGPGAPK(LOGL_DEBUG, "PQ: Adding codec %s, %s format %s\n", codec->name, enc_dec_n ? "encoding to" : "decoding from", fmt->name); if (!item->proc) diff --git a/src/pq_file.c b/src/pq_file.c index 2d0a6b0..b31ef52 100644 --- a/src/pq_file.c +++ b/src/pq_file.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -97,7 +98,7 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in int osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding file input (blk_len=%u)\n", blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding file input (blk_len=%u)\n", blk_len); return pq_queue_file_op(pq, src, blk_len, 1); } @@ -110,6 +111,6 @@ osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int b int osmo_gapk_pq_queue_file_output(struct osmo_gapk_pq *pq, FILE *dst, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding file output (blk_len=%u)\n", blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding file output (blk_len=%u)\n", blk_len); return pq_queue_file_op(pq, dst, blk_len, 0); } diff --git a/src/pq_format.c b/src/pq_format.c index 08860a3..ae5386e 100644 --- a/src/pq_format.c +++ b/src/pq_format.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -44,7 +45,8 @@ osmo_gapk_pq_queue_fmt_convert(struct osmo_gapk_pq *pq, const struct osmo_gapk_f codec = osmo_gapk_codec_get_from_type(fmt->codec_type); if (!codec) { - fprintf(stderr, "[!] Cannot determine codec from format %s\n", fmt->name); + LOGPGAPK(LOGL_ERROR, "Cannot determine codec from " + "format %s\n", fmt->name); return -EINVAL; } @@ -54,14 +56,14 @@ osmo_gapk_pq_queue_fmt_convert(struct osmo_gapk_pq *pq, const struct osmo_gapk_f return -ENOMEM; if (to_from_n) { - fprintf(stderr, "[+] PQ: Adding conversion from canon to %s (for codec %s)\n", - fmt->name, codec->name); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding conversion from canon " + "to %s (for codec %s)\n", fmt->name, codec->name); item->len_in = codec->canon_frame_len; item->len_out = fmt->frame_len; item->state = fmt->conv_from_canon; } else { - fprintf(stderr, "[+] PQ: Adding conversion from %s to canon (for codec %s)\n", - fmt->name, codec->name); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding conversion from %s " + "to canon (for codec %s)\n", fmt->name, codec->name); item->len_in = fmt->frame_len; item->len_out = codec->canon_frame_len; item->state = fmt->conv_to_canon; diff --git a/src/pq_rtp.c b/src/pq_rtp.c index b68e84a..6e7a87d 100644 --- a/src/pq_rtp.c +++ b/src/pq_rtp.c @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -86,7 +87,8 @@ struct pq_state_rtp { uint32_t ssrc; }; -#define rtp_err(x, args...) fprintf(stderr, "[!] %s():" x, __func__, ## args) +#define rtp_err(err_msg, args...) \ + LOGPGAPK(LOGL_ERROR, "%s():" err_msg, __func__, ## args) static int pq_cb_rtp_input(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len) @@ -241,7 +243,7 @@ pq_queue_rtp_op(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len, int i int osmo_gapk_pq_queue_rtp_input(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding RTP input (blk_len=%u)\n", blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding RTP input (blk_len=%u)\n", blk_len); return pq_queue_rtp_op(pq, udp_fd, blk_len, 1); } @@ -253,6 +255,6 @@ osmo_gapk_pq_queue_rtp_input(struct osmo_gapk_pq *pq, int udp_fd, unsigned int b int osmo_gapk_pq_queue_rtp_output(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len) { - fprintf(stderr, "[+] PQ: Adding RTP output (blk_len=%u)\n", blk_len); + LOGPGAPK(LOGL_DEBUG, "PQ: Adding RTP output (blk_len=%u)\n", blk_len); return pq_queue_rtp_op(pq, udp_fd, blk_len, 0); } diff --git a/src/procqueue.c b/src/procqueue.c index 00472d3..ffd3f6d 100644 --- a/src/procqueue.c +++ b/src/procqueue.c @@ -24,6 +24,7 @@ #include #include +#include /* crate a new (empty) processing queue */ struct osmo_gapk_pq * @@ -102,7 +103,7 @@ osmo_gapk_pq_prepare(struct osmo_gapk_pq *pq) llist_for_each_entry(item, &pq->items, list) { /* Make sure I/O data lengths are equal */ if (item->len_in && item->len_in != len_prev) { - fprintf(stderr, "[!] PQ item requires input size %u, " + LOGPGAPK(LOGL_ERROR, "PQ item requires input size %u, " "but previous output is %u\n", item->len_in, len_prev); return -EINVAL; } @@ -151,8 +152,8 @@ osmo_gapk_pq_execute(struct osmo_gapk_pq *pq) /* Call item's processing handler */ rv = item->proc(item->state, item->buf, buf_prev, len_prev); if (rv < 0) { - fprintf(stderr, "[!] osmo_gapk_pq_execute(): " - "abort, item returned %d\n", rv); + LOGPGAPK(LOGL_ERROR, "Queue execution aborted: " + "item returned %d\n", rv); return rv; }