Textify logging for binary protocols

r2286 | dcl | 2008-01-21 09:54:20 +1100 (Mon, 21 Jan 2008) | 2 lines
This commit is contained in:
Douglas Clowes
2008-01-21 09:54:20 +11:00
parent 168e0d1bfa
commit 6016d8d46b
2 changed files with 33 additions and 5 deletions

View File

@@ -1,5 +1,27 @@
#include "fsm.h"
char* fsm_textify(const char* pInp, int inp_len, char* pOut, int out_len) {
int iInp = 0;
int iOut = 0;
for (iInp = 0; iInp < inp_len; ++iInp) {
if ((pInp[iInp] < 32 || pInp[iInp] > 126)) {
if (iOut < out_len - 4) {
const char hex[] = "0123456789abcdef";
pOut[iOut++] = '\\';
pOut[iOut++] = hex[(pInp[iInp] >> 4) & 0xf];
pOut[iOut++] = hex[pInp[iInp] & 0xf];
}
else if (iOut < out_len - 1)
pOut[iOut++] = '.';
}
else if (iOut < out_len - 1)
pOut[iOut++] = pInp[iInp];
}
if (iOut < out_len)
pOut[iOut++] = '\0';
return pOut;
}
static void report_event(pStateMachine self, pEvtEvent event) {
char line[132];
char text[132];
@@ -25,7 +47,9 @@ int fsm_msg_callback(pAsyncTxn pCmd)
if (pCmd->txn_status == ATX_TIMEOUT) {
if (self->debug) {
SICSLogWrite(pCmd->out_buf, eStatus);
char line[1024];
fsm_textify(pCmd->out_buf, pCmd->out_len, line, sizeof(line));
SICSLogWrite(line, eStatus);
SICSLogWrite("<TIMEOUT>", eStatus);
}
event.event_type = eTimeoutEvent;
@@ -33,8 +57,11 @@ int fsm_msg_callback(pAsyncTxn pCmd)
}
else {
if (self->debug) {
SICSLogWrite(pCmd->out_buf, eStatus);
SICSLogWrite(pCmd->inp_buf, eStatus);
char line[1024];
fsm_textify(pCmd->out_buf, pCmd->out_len, line, sizeof(line));
SICSLogWrite(line, eStatus);
fsm_textify(pCmd->inp_buf, pCmd->inp_idx, line, sizeof(line));
SICSLogWrite(line, eStatus);
}
event.event_type = eMessageEvent;
event.event.msg.cmd = pCmd;
@@ -68,8 +95,7 @@ void fsm_change_state(pStateMachine self, StateFunc func) {
self->state_name ? self->state_name(self->myState) : "<unknown>",
self->mySubState,
self->state_name ? self->state_name(func) : "<unknown>");
if (self->debug)
SICSLogWrite(line, eStatus);
SICSLogWrite(line, eStatus);
}
self->myPrevState = self->myState;
self->myState = func;

View File

@@ -53,6 +53,8 @@ struct statemachine_s {
const char* (*event_name)(pEvtEvent event, char* text, int length);
};
char* fsm_textify(const char* pInp, int inp_len, char* pOut, int out_len);
int fsm_msg_callback(pAsyncTxn pCmd);
int fsm_tmr_callback(void* ctx, int mode);
int fsm_cmd_execute(pStateMachine self, const void* cmd);