Skip to content

Commit

Permalink
Ring-buffer for debug messages. Complicated (see full message).
Browse files Browse the repository at this point in the history
When debugging is disabled, messages with a prio of DEBUG get stored in d_buf,
 which is a ring buffer of 64 lines; on enabling debugging, the contents of
 this buffer are flushed to the (status) tab.
Consequently, if something strange happens and you want to debug it, you can
 read all the debugging info about what just happened; but all that debugging
 info won't fill up buf 0 and cause visible messages to disappear from the
 (status) tab when debugging is disabled.
  • Loading branch information
ec429 committed Mar 2, 2012
1 parent 0a18ee9 commit e184c8b
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 123 deletions.
144 changes: 85 additions & 59 deletions buffer.c
Expand Up @@ -8,75 +8,79 @@

#include "buffer.h"

int init_start_buffer(void)
int init_ring(ring *r)
{
s_buf.nlines=0;
s_buf.lt=NULL;
s_buf.lm=NULL;
s_buf.ts=NULL;
s_buf.errs=0;
r->nlines=64;
r->ptr=0;
r->filled=false;
r->loop=false;
r->lt=malloc(r->nlines*sizeof(char *));
r->ltag=malloc(r->nlines*sizeof(char *));
r->lm=malloc(r->nlines*sizeof(mtype));
r->ts=malloc(r->nlines*sizeof(time_t));
r->errs=0;
return(0);
}

int add_to_start_buffer(mtype lm, const char *lt)
int add_to_ring(ring *r, mtype lm, const char *lt, const char *ltag)
{
s_buf.nlines++;
char **nlt;mtype *nlm;time_t *nts;
nlt=(char **)realloc(s_buf.lt, s_buf.nlines*sizeof(char *));
if(!nlt)
if(!r->nlines) init_ring(r);
int p=r->ptr;
if(!(++r->ptr<r->nlines))
{
s_buf.nlines--;
s_buf.errs++;
return(1);
}
s_buf.lt=nlt;
s_buf.lt[s_buf.nlines-1]=strdup(lt);
nlm=(mtype *)realloc(s_buf.lm, s_buf.nlines*sizeof(mtype));
if(!nlm)
{
s_buf.nlines--;
s_buf.errs++;
return(1);
}
s_buf.lm=nlm;
s_buf.lm[s_buf.nlines-1]=lm;
nts=(time_t *)realloc(s_buf.ts, s_buf.nlines*sizeof(time_t));
if(!nts)
{
s_buf.nlines--;
s_buf.errs++;
return(1);
if(r->loop)
{
r->filled=true;
r->ptr-=r->nlines;
}
else
{
r->ptr=p;
r->errs++;
return(1);
}
}
s_buf.ts=nts;
s_buf.ts[s_buf.nlines-1]=time(NULL);
r->lt[p]=strdup(lt);
r->ltag[p]=strdup(ltag);
r->lm[p]=lm;
r->ts[p]=time(NULL);
return(0);
}

int asb_failsafe(mtype lm, const char *lt)
int atr_failsafe(ring *r, mtype lm, const char *lt, const char *ltag)
{
int e=0;
if((e=add_to_start_buffer(lm, lt)))
if((e=add_to_ring(r, lm, lt, ltag)))
{
fprintf(stderr, "init[%d]: %s\n", e, lt);
fprintf(stderr, "atr[%d]: %s%s\n", e, ltag, lt);
}
return(e);
}

int free_start_buffer(void)
int free_ring(ring *r)
{
int i;
if(s_buf.lt)
if(r->lt)
{
for(i=0;i<(r->filled?r->nlines:r->ptr);i++)
{
free(r->lt[i]);
}
free(r->lt);
r->lt=NULL;
}
if(r->ltag)
{
for(i=0;i<s_buf.nlines;i++)
for(i=0;i<(r->filled?r->nlines:r->ptr);i++)
{
free(s_buf.lt[i]);
free(r->ltag[i]);
}
free(s_buf.lt);
s_buf.lt=NULL;
free(r->ltag);
r->ltag=NULL;
}
free(s_buf.lm);
free(s_buf.ts);
s_buf.nlines=0;
free(r->lm);
free(r->ts);
r->nlines=0;
return(0);
}

Expand All @@ -92,6 +96,8 @@ int initialise_buffers(int buflines)
bufs[0].nick=nick;
bufs[0].ilist=igns;
add_to_buffer(0, STA, QUIET, 0, false, GPL_TAIL, "quirc -- ");
init_ring(&d_buf);
d_buf.loop=true;
return(0);
}

Expand Down Expand Up @@ -257,6 +263,15 @@ int add_to_buffer(int buf, mtype lm, prio lq, char lp, bool ls, const char *lt,
}
return(1);
}
if(!debug&&(lq==DEBUG))
{
if(!d_buf.nlines)
{
init_ring(&d_buf);
d_buf.loop=true;
}
return(add_to_ring(&d_buf, lm, lt, ltag));
}
int optr=bufs[buf].ptr;
bool scrollisptr=(bufs[buf].scroll==bufs[buf].ptr)&&(bufs[buf].ascroll==0);
bufs[buf].lm[bufs[buf].ptr]=lm;
Expand Down Expand Up @@ -936,40 +951,51 @@ int e_buf_print(int buf, mtype lm, message pkt, const char *lead)
return(add_to_buffer(buf, lm, QUIET, 0, false, text, lead));
}

int transfer_start_buffer(void)
int transfer_ring(ring *r, prio lq)
{
int i,e=0;
for(i=0;i<s_buf.nlines;i++)
if(r->filled)
{
e|=add_to_buffer(0, s_buf.lm[i], QUIET, 0, false, s_buf.lt[i], "init: ");
for(i=0;i<r->nlines;i++)
{
int j=(i+r->ptr)%r->nlines;
e|=add_to_buffer(0, r->lm[j], lq, 0, false, r->lt[j], r->ltag[j]);
}
}
else
{
for(i=0;i<r->ptr;i++)
{
e|=add_to_buffer(0, r->lm[i], lq, 0, false, r->lt[i], r->ltag[i]);
}
}
return(e);
}

int push_buffer(void)
int push_ring(ring *r, prio lq)
{
if(!bufs || transfer_start_buffer())
if(!bufs || transfer_ring(r, lq))
{
if(bufs) add_to_buffer(0, ERR, NORMAL, 0, false, "Failed to transfer start_buffer", "init[xsb]: ");
if(bufs) add_to_buffer(0, ERR, NORMAL, 0, false, "Failed to transfer ring", "init[xr]: ");
int i;
for(i=0;i<s_buf.nlines;i++)
for(i=0;i<s_buf.ptr;i++)
{
fprintf(stderr, "init[xsb]: %s\n", s_buf.lt[i]);
fprintf(stderr, "init[xr]: %s%s\n", r->ltag[i], r->lt[i]);
}
if(bufs)
{
char msg[32];
sprintf(msg, "%d messages written to stderr", s_buf.nlines);
add_to_buffer(0, STA, NORMAL, 0, false, msg, "init[xsb]: ");
sprintf(msg, "%d messages written to stderr", r->nlines);
add_to_buffer(0, STA, NORMAL, 0, false, msg, "init[xr]: ");
}
}
if(bufs&&s_buf.errs)
if(bufs&&r->errs)
{
char msg[80];
sprintf(msg, "%d messages were written to stderr due to start_buffer errors", s_buf.errs);
sprintf(msg, "%d messages were written to stderr due to ring errors", r->errs);
add_to_buffer(0, ERR, NORMAL, 0, false, msg, "init[errs]: ");
}
return(free_start_buffer());
return(free_ring(r));
}

void titlebar(void)
Expand Down
22 changes: 14 additions & 8 deletions buffer.h
Expand Up @@ -113,20 +113,26 @@ int nbufs;
int cbuf;
buffer *bufs;

struct
typedef struct
{
int nlines;
int ptr;
bool filled;
char **lt;
char **ltag;
mtype *lm;
time_t *ts;
bool loop;
int errs;
}
s_buf;
ring;

int init_start_buffer(void);
int add_to_start_buffer(mtype lm, const char *lt);
int asb_failsafe(mtype lm, const char *lt);
int free_start_buffer(void);
ring s_buf, d_buf;

int init_ring(ring *r);
int add_to_ring(ring *r, mtype lm, const char *lt, const char *ltag);
int atr_failsafe(ring *r, mtype lm, const char *lt, const char *ltag);
int free_ring(ring *r);
int initialise_buffers(int buflines);
int init_buffer(int buf, btype type, const char *bname, int nlines);
int free_buffer(int buf);
Expand All @@ -136,8 +142,8 @@ int redraw_buffer(void);
int render_buffer(int buf);
int render_line(int buf, int uline);
int e_buf_print(int buf, mtype lm, message pkt, const char *lead);
int transfer_start_buffer(void);
int push_buffer(void);
int transfer_ring(ring *r, prio lq);
int push_ring(ring *r, prio lq);
void in_update(iline inp);
char *highlight(const char *src); // use colours to highlight \escapes. Returns a malloc-like pointer
void titlebar(void);
Expand Down
28 changes: 14 additions & 14 deletions config.c
Expand Up @@ -27,7 +27,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: missing mod in %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
free(line);
continue;
}
Expand All @@ -36,7 +36,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: odd mod length in %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
free(line);
continue;
}
Expand All @@ -45,7 +45,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: malloc failure in %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
free(line);
continue;
}
Expand All @@ -56,7 +56,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: bad mod (not hex) in %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
free(line);
cont=true;
break;
Expand All @@ -67,7 +67,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: sscanf failed in %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
free(line);
cont=true;
break;
Expand All @@ -90,7 +90,7 @@ void loadkeys(FILE *fp)
{
char msg[32+strlen(new.name)];
sprintf(msg, "keys: unrecognised name %s", new.name);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
}
free(new.mod);
}
Expand Down Expand Up @@ -217,7 +217,7 @@ int rcread(FILE *rcfp)
{
char msg[48+strlen(cmd)];
sprintf(msg, "rc: Unrecognised ident in %%colour (%s)", cmd);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
nerrors++;
}
}
Expand All @@ -230,7 +230,7 @@ int rcread(FILE *rcfp)
{
char msg[40+strlen(cmd)];
sprintf(msg, "rc: Command (%s) without argument", cmd);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
nerrors++;
}
else if(strcmp(cmd, "server")==0)
Expand Down Expand Up @@ -296,15 +296,15 @@ int rcread(FILE *rcfp)
char *sw=strtok(rest, " \t");
if(*sw!='-')
{
asb_failsafe(ERR, "rc: ignore: need options (use '-' for no options)");
atr_failsafe(&s_buf, ERR, "rc: ignore: need options (use '-' for no options)", "init: ");
nerrors++;
}
else
{
rest=strtok(NULL, "");
if(!rest)
{
asb_failsafe(ERR, "rc: ignore: need options (use '-' for no options)");
atr_failsafe(&s_buf, ERR, "rc: ignore: need options (use '-' for no options)", "init: ");
nerrors++;
}
else
Expand Down Expand Up @@ -346,15 +346,15 @@ int rcread(FILE *rcfp)
char *sw=strtok(rest, " \t");
if(*sw!='-')
{
asb_failsafe(ERR, "rc: *ignore: need options (use '-' for no options)");
atr_failsafe(&s_buf, ERR, "rc: *ignore: need options (use '-' for no options)", "init: ");
nerrors++;
}
else
{
rest=strtok(NULL, "");
if(!rest)
{
asb_failsafe(ERR, "rc: *ignore: need options (use '-' for no options)");
atr_failsafe(&s_buf, ERR, "rc: *ignore: need options (use '-' for no options)", "init: ");
nerrors++;
}
else
Expand Down Expand Up @@ -411,7 +411,7 @@ int rcread(FILE *rcfp)
{
char msg[48+strlen(cmd)];
sprintf(msg, "rc: Unrecognised cmd %s in .quirc (ignoring)", cmd);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
nerrors++;
}
}
Expand Down Expand Up @@ -510,7 +510,7 @@ signed int pargs(int argc, char *argv[])
{
char msg[40+strlen(argv[arg])];
sprintf(msg, "pargs: Unrecognised argument '%s'", argv[arg]);
asb_failsafe(ERR, msg);
atr_failsafe(&s_buf, ERR, msg, "init: ");
}
}
if(check) return(0);
Expand Down

0 comments on commit e184c8b

Please sign in to comment.