X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Flog.c;h=03dd21f0124f904225057650de63a6da7ff43bab;hb=77463bd397127b8a4e7266e601b4cdf87825beeb;hp=e12c8f4ccaa6a63186dfd6d4958047d676908a00;hpb=6d9da8efbf1429301922eb7ab1551866362544ab;p=libsigrok.git diff --git a/src/log.c b/src/log.c index e12c8f4c..03dd21f0 100644 --- a/src/log.c +++ b/src/log.c @@ -14,12 +14,13 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ +#include #include #include +#include #include #include "libsigrok-internal.h" @@ -55,13 +56,6 @@ static sr_log_callback sr_log_cb = sr_logv; */ static void *sr_log_cb_data = NULL; -/* Log domain (a short string that is used as prefix for all messages). */ -/** @cond PRIVATE */ -#define LOGDOMAIN_MAXLEN 30 -#define LOGDOMAIN_DEFAULT "sr: " -/** @endcond */ -static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT; - /** @cond PRIVATE */ #define LOGLEVEL_TIMESTAMP SR_LOG_DBG /** @endcond */ @@ -113,54 +107,6 @@ SR_API int sr_log_loglevel_get(void) return cur_loglevel; } -/** - * Set the libsigrok logdomain string. - * - * @param logdomain The string to use as logdomain for libsigrok log - * messages from now on. Must not be NULL. The maximum - * length of the string is 30 characters (this does not - * include the trailing NUL-byte). Longer strings are - * truncated. - * In order to not use a logdomain, pass an empty string. - * The function makes its own copy of the input string, i.e. - * the caller does not need to keep it around. - * - * @retval SR_OK upon success. - * @retval SR_ERR_ARG @a logdomain was NULL. - * @retval SR_ERR @a logdomain was truncated. - * - * @since 0.1.0 - */ -SR_API int sr_log_logdomain_set(const char *logdomain) -{ - size_t len; - - if (!logdomain) { - sr_err("%s: logdomain was NULL", __func__); - return SR_ERR_ARG; - } - - len = g_strlcpy(sr_log_domain, logdomain, sizeof sr_log_domain); - - sr_dbg("Log domain set to '%s'.", sr_log_domain); - - return (len < sizeof sr_log_domain) ? SR_OK : SR_ERR; -} - -/** - * Get the currently configured libsigrok logdomain. - * - * @return A copy of the currently configured libsigrok logdomain - * string. The caller is responsible for g_free()ing the string when - * it is no longer needed. - * - * @since 0.1.0 - */ -SR_API char *sr_log_logdomain_get(void) -{ - return g_strdup(sr_log_domain); -} - /** * Set the libsigrok log callback to the specified function. * @@ -216,6 +162,8 @@ static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args { uint64_t elapsed_us, minutes; unsigned int rest_us, seconds, microseconds; + char *raw_output, *output; + int raw_len, raw_idx, idx, ret; /* This specific log callback doesn't need the void pointer data. */ (void)cb_data; @@ -232,17 +180,31 @@ static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args seconds = rest_us / G_TIME_SPAN_SECOND; microseconds = rest_us % G_TIME_SPAN_SECOND; - if (fprintf(stderr, "[%.2" PRIu64 ":%.2u.%.6u] ", - minutes, seconds, microseconds) < 0) - return SR_ERR; + ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ", + minutes, seconds, microseconds); + } else { + ret = fputs("sr: ", stderr); } - if (sr_log_domain[0] != '\0' && fputs(sr_log_domain, stderr) < 0) - return SR_ERR; - if (vfprintf(stderr, format, args) < 0) - return SR_ERR; - if (putc('\n', stderr) < 0) + + if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0) return SR_ERR; + output = g_malloc0(raw_len + 1); + + /* Copy the string without any unwanted newlines. */ + raw_idx = idx = 0; + while (raw_idx < raw_len) { + if (raw_output[raw_idx] != '\n') { + output[idx] = raw_output[raw_idx]; + idx++; + } + raw_idx++; + } + + g_fprintf(stderr, "%s\n", output); + g_free(raw_output); + g_free(output); + return SR_OK; }