Add an externally controllable message-log and redirect tg-cli logging to the libpurple logging mechanism

This commit is contained in:
mjentsch 2014-06-09 14:22:06 +02:00
parent 58244abb2d
commit 93f4bd25ae
6 changed files with 116 additions and 10 deletions

View file

@ -13,10 +13,10 @@ EXTRA_LIBS=@LIBS@ @EXTRA_LIBS@
LOCAL_LDFLAGS=-rdynamic -ggdb ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h ${srcdir}/tools.h ${srcdir}/lua-tg.h
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h ${srcdir}/tools.h ${srcdir}/lua-tg.h ${srcdir}/msglog.h
INCLUDE=-I. -I${srcdir}
CC=@CC@
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o msglog.o
.SUFFIXES:

View file

@ -49,6 +49,7 @@
// libpurple debugging-messages
#include "debug.h"
#include "purple-plugin/telegram-purple.h"
#include "msglog.h"
#define ALLOW_MULT 1
char *default_prompt = "> ";
@ -1191,20 +1192,22 @@ void hexdump (int *in_ptr, int *in_end) {
}
void logprintf (const char *format, ...) {
va_list ap;
va_start (ap, format);
log_message(format, ap);
va_end (ap);
/*
int x = 0;
if (!prompt_was) {
x = 1;
print_start ();
}
printf (COLOR_GREY " *** ");
va_list ap;
va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);
printf (COLOR_NORMAL);
if (x) {
print_end ();
}
*/
}
int color_stack_pos;

67
msglog.c Normal file
View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdarg.h>
#define COLOR_RED "\033[0;31m"
#define COLOR_REDB "\033[1;31m"
#define COLOR_NORMAL "\033[0m"
#define COLOR_GREEN "\033[32;1m"
#define COLOR_GREY "\033[37;1m"
#define COLOR_YELLOW "\033[33;1m"
#define COLOR_BLUE "\033[34;1m"
#define COLOR_MAGENTA "\033[35;1m"
#define COLOR_CYAN "\033[36;1m"
#define COLOR_LCYAN "\033[0;36m"
#define COLOR_INVERSE "\033[7m"
void log_printf(const char *format, ...)
{
printf (COLOR_GREY " *** ");
va_list ap;
va_start (ap, format);
vfprintf (stdout, format, ap);
va_end (ap);
printf (COLOR_NORMAL);
}
/**
* The callback that will log the given msg to the used target
*/
void (*log_cb)(const char*, ...) = log_printf;
/**
* Set a custom logging callback to use instead of the regular
* printf to stdout
*/
void set_log_cb(void (*cb)(const char*, ...))
{
log_cb = cb;
}
/**
* Log a message to the current message log
*/
void log_message(const char *format, ...)
{
va_list ap;
va_start (ap, format);
log_cb(format, ap);
va_end (ap);
}
/*
TODO: implement different log levels
void log_debug(const char* format, ...)
{
}
void log_warning(const char* format, ...)
{
}
void log_error(const char* format, ...)
{
}
*/

17
msglog.h Normal file
View file

@ -0,0 +1,17 @@
/**
* Set a custom logging callback to use instead of regular printing
* to stdout
*/
void set_log_cb(void (*cb)(const char*, ...));
/**
* Log a message to the current message log
*/
void log_message(const char *format, ...);
/*
void log_debug(const char* format, ...);
void log_warning(const char* format, ...);
void log_error(const char* format, ...);
*/

View file

@ -4,7 +4,7 @@
EXTRA_LIBS = -lcrypto -lz -lm -lreadline
LDFLAGSTG = -L/usr/local/lib
CPPFLAGSTG = -I/usr/local/include
OBJECTSTG = ../main.o ../loop.o ../interface.o ../net.o ../mtproto-common.o ../mtproto-client.o ../queries.o ../structures.o ../binlog.o ../tools.o ../lua-tg.o
OBJECTSTG = ../main.o ../loop.o ../interface.o ../net.o ../mtproto-common.o ../mtproto-client.o ../queries.o ../structures.o ../binlog.o ../tools.o ../lua-tg.o ../msglog.o
INCLUDE = -I. -I..
#

View file

@ -16,10 +16,13 @@
#include <glib.h>
#include <string.h>
#include <stdarg.h>
// test
// Libpurple Plugin Includes
#include "notify.h"
#include "plugin.h" // NEEDED?
#include "plugin.h"
#include "version.h"
#include "account.h"
#include "accountopt.h"
@ -37,13 +40,26 @@
// Telegram Includes
#include <tg-cli.h>
#include "msglog.h"
// telegram-purple includes
#include "loop.h"
#include "telegram-purple.h"
static PurplePlugin *_telegram_protocol = NULL;
/**
* Redirect the msglog of the telegram-cli application to the libpurple
* logger
*/
void tg_cli_log_cb(const char* format, ...)
{
va_list ap;
va_start (ap, format);
purple_debug_info(PLUGIN_ID, format, ap);
va_end (ap);
}
/**
* Returns the base icon name for the given buddy and account.
* If buddy is NULL and the account is non-NULL, it will return the
@ -522,10 +538,13 @@ static PurplePluginProtocolInfo prpl_info = {
static void tgprpl_init(PurplePlugin *plugin)
{
PurpleAccountOption *option;
PurpleAccountOption *option;
PurpleAccountUserSplit *split;
GList *verification_values = NULL;
// intialise logging
set_log_cb(&tg_cli_log_cb);
// Required Verification-Key
// split = purple_account_user_split_new("Verification key", NULL, '@');
// purple_account_user_split_set_reverse(split, FALSE);