Browse Source

Implemented logger callback on mailstream to provide a better logging system.

dvh-chacham15-master
Hoa V. DINH 9 years ago
parent
commit
7e51aed056
5 changed files with 157 additions and 13 deletions
  1. +22
    -3
      src/data-types/mailstream.c
  2. +3
    -0
      src/data-types/mailstream.h
  3. +108
    -9
      src/data-types/mailstream_low.c
  4. +3
    -0
      src/data-types/mailstream_low.h
  5. +21
    -1
      src/data-types/mailstream_types.h

+ 22
- 3
src/data-types/mailstream.c View File

@ -74,11 +74,16 @@ mailstream * mailstream_new(mailstream_low * low, size_t buffer_size)
s->write_buffer_len = 0;
s->buffer_max_size = buffer_size;
s->low = low;
s->low = NULL;
s->idle = NULL;
s->idling = 0;
s->logger = NULL;
s->logger_context = NULL;
mailstream_set_low(s, low);
return s;
free_read_buffer:
@ -274,9 +279,18 @@ mailstream_low * mailstream_get_low(mailstream * s)
return s->low;
}
static void low_logger(mailstream_low * s, int log_type,
const char * str, size_t size, void * context)
{
if (s->logger != NULL) {
s->logger(context, log_type, str, size, s->logger_context);
}
}
void mailstream_set_low(mailstream * s, mailstream_low * low)
{
s->low = low;
mailstream_low_set_logger(low, low_logger, s);
}
int mailstream_close(mailstream * s)
@ -296,8 +310,6 @@ int mailstream_close(mailstream * s)
return 0;
}
ssize_t mailstream_feed_read_buffer(mailstream * s)
{
ssize_t read_bytes;
@ -451,3 +463,10 @@ void mailstream_unsetup_idle(mailstream * s)
s->idling = 0;
}
void mailstream_set_logger(mailstream * s, void (* logger)(mailstream * s, int log_type,
const char * str, size_t size, void * context), void * logger_context)
{
s->logger = logger;
s->logger_context = logger_context;
}

+ 3
- 0
src/data-types/mailstream.h View File

@ -86,6 +86,9 @@ extern void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int
const char * str, size_t size);
#endif
void mailstream_set_logger(mailstream * s, void (* logger)(mailstream * s, int log_type,
const char * str, size_t size, void * context), void * logger_context);
/* can be run in thread */
int mailstream_wait_idle(mailstream * s, int max_idle_delay);


+ 108
- 9
src/data-types/mailstream_low.c View File

@ -68,11 +68,17 @@ LIBETPAN_EXPORT
void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int direction,
const char * str, size_t size) = NULL;
static inline void mailstream_logger_internal(mailstream_low * s, int is_stream_data, int direction,
const char * buffer, size_t size);
#define STREAM_LOG_ERROR(low, direction, buf, size) \
if (low->logger != NULL) { \
mailstream_logger_internal(low, 2, direction, buf, size); \
} \
if (mailstream_debug) { \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 2, direction, buf, size); \
} \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 2, direction, buf, size); \
} \
else if (mailstream_logger != NULL) { \
mailstream_logger(direction, buf, size); \
} \
@ -94,10 +100,13 @@ void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int direct
}
#define STREAM_LOG_BUF(low, direction, buf, size) \
if (low->logger != NULL) { \
mailstream_logger_internal(low, 1, direction, buf, size); \
} \
if (mailstream_debug) { \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 1, direction, buf, size); \
} \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 1, direction, buf, size); \
} \
else if (mailstream_logger != NULL) { \
mailstream_logger(direction, buf, size); \
} \
@ -119,10 +128,13 @@ void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int direct
}
#define STREAM_LOG(low, direction, str) \
if (low->logger != NULL) { \
mailstream_logger_internal(low, 0, direction, buf, strlen(str)); \
} \
if (mailstream_debug) { \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 0, direction, str, strlen(str)); \
} \
if (mailstream_logger_id != NULL) { \
mailstream_logger_id(low, 0, direction, str, strlen(str)); \
} \
else if (mailstream_logger != NULL) { \
mailstream_logger(direction, str, strlen(str)); \
} \
@ -145,6 +157,7 @@ void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int direct
#else
#define STREAM_LOG_ERROR(low, direction, buf, size) do { } while (0)
#define STREAM_LOG_BUF(low, direction, buf, size) do { } while (0)
#define STREAM_LOG(low, direction, buf) do { } while (0)
@ -167,6 +180,8 @@ mailstream_low * mailstream_low_new(void * data,
s->privacy = 1;
s->identifier = NULL;
s->timeout = 0;
s->logger = NULL;
s->logger_context = NULL;
return s;
}
@ -305,3 +320,87 @@ time_t mailstream_low_get_timeout(mailstream_low * s)
return s->timeout;
}
void mailstream_low_set_logger(mailstream_low * s, void (* logger)(mailstream_low * s, int log_type,
const char * str, size_t size, void * context), void * logger_context)
{
s->logger = logger;
s->logger_context = logger_context;
}
static inline void mailstream_logger_internal(mailstream_low * s, int is_stream_data, int direction,
const char * buffer, size_t size)
{
int log_type = -1;
/*
stream data:
0: log
1: buffer
2: error
direction:
4|1: send error
4: receive error
2: sent private data
1: sent data
0: received data
*/
switch (is_stream_data) {
case 0: {
switch (direction) {
case 1:
case 2:
case 4|1:
log_type = MAILSTREAM_LOG_TYPE_INFO_SENT;
break;
case 0:
case 4:
log_type = MAILSTREAM_LOG_TYPE_INFO_RECEIVED;
break;
default:
log_type = MAILSTREAM_LOG_TYPE_INFO_GENERIC;
break;
}
}
case 1: {
switch (direction) {
case 2:
log_type = MAILSTREAM_LOG_TYPE_DATA_SENT_PRIVATE;
break;
case 1:
case 4|1:
log_type = MAILSTREAM_LOG_TYPE_DATA_SENT;
break;
case 0:
case 4:
default:
log_type = MAILSTREAM_LOG_TYPE_DATA_RECEIVED;
break;
}
break;
}
case 2: {
switch (direction) {
case 2:
case 1:
case 4|1:
log_type = MAILSTREAM_LOG_TYPE_ERROR_SENT;
break;
case 0:
case 4:
log_type = MAILSTREAM_LOG_TYPE_ERROR_RECEIVED;
break;
default:
log_type = MAILSTREAM_LOG_TYPE_ERROR_GENERIC;
break;
}
break;
}
}
if (log_type == -1)
return;
s->logger(s, log_type, buffer, size, s->logger_context);
}

+ 3
- 0
src/data-types/mailstream_low.h View File

@ -89,6 +89,9 @@ void mailstream_low_set_timeout(mailstream_low * s,
time_t mailstream_low_get_timeout(mailstream_low * s);
void mailstream_low_set_logger(mailstream_low * s, void (* logger)(mailstream_low * s, int log_type,
const char * str, size_t size, void * context), void * logger_context);
#ifdef __cplusplus
}
#endif


+ 21
- 1
src/data-types/mailstream_types.h View File

@ -43,7 +43,7 @@ extern "C" {
#define LIBETPAN_MAILSTREAM_DEBUG
#ifndef LIBETPAN_CONFIG_H
# include <libetpan/libetpan-config.h>
# include <libetpan/libetpan-config.h>
#endif
struct _mailstream;
@ -54,6 +54,20 @@ struct _mailstream_low;
typedef struct _mailstream_low mailstream_low;
enum {
MAILSTREAM_LOG_TYPE_INFO_GENERIC,
MAILSTREAM_LOG_TYPE_INFO_RECEIVED,
MAILSTREAM_LOG_TYPE_INFO_SENT,
MAILSTREAM_LOG_TYPE_ERROR_GENERIC,
MAILSTREAM_LOG_TYPE_ERROR_RECEIVED,
MAILSTREAM_LOG_TYPE_ERROR_SENT,
MAILSTREAM_LOG_TYPE_DATA_RECEIVED,
MAILSTREAM_LOG_TYPE_DATA_SENT,
MAILSTREAM_LOG_TYPE_DATA_SENT_PRIVATE,
};
struct _mailstream {
size_t buffer_max_size;
@ -67,6 +81,9 @@ struct _mailstream {
struct mailstream_cancel * idle;
int idling;
void (* logger)(mailstream * s, int log_type,
const char * str, size_t size, void * logger_context);
void * logger_context;
};
struct mailstream_low_driver {
@ -87,6 +104,9 @@ struct _mailstream_low {
int privacy;
char * identifier;
unsigned long timeout; /* in seconds, 0 will use the global value */
void (* logger)(mailstream_low * s, int log_type,
const char * str, size_t size, void * logger_context);
void * logger_context;
};
typedef void progress_function(size_t current, size_t maximum);


Loading…
Cancel
Save