From 5923c452f896d55b718be7dab87aa38daf91bdfe Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 14 Dec 2019 16:18:35 +0000 Subject: [PATCH] logging functions as error printer --- hawk/lib/hawk.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/hawk/lib/hawk.c b/hawk/lib/hawk.c index e53ee710..7bde81a1 100644 --- a/hawk/lib/hawk.c +++ b/hawk/lib/hawk.c @@ -172,11 +172,7 @@ int hawk_init (hawk_t* awk, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr, const hawk_prm if (init_token(awk, &awk->ptok) <= -1 || init_token(awk, &awk->tok) <= -1 || - init_token(awk, &awk->ntok) <= -1) - { - hawk_seterrnum (awk, HAWK_ENOMEM, HAWK_NULL); - goto oops; - } + init_token(awk, &awk->ntok) <= -1) goto oops; awk->opt.trait = HAWK_MODERN; #if defined(__OS2__) || defined(_WIN32) || defined(__DOS__) @@ -186,6 +182,10 @@ int hawk_init (hawk_t* awk, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr, const hawk_prm awk->opt.log_mask = HAWK_LOG_ALL_LEVELS | HAWK_LOG_ALL_TYPES; awk->opt.log_maxcapa = HAWK_DFL_LOG_MAXCAPA; + awk->log.capa = HAWK_ALIGN_POW2(1, HAWK_LOG_CAPA_ALIGN); + awk->log.ptr = hawk_allocmem(awk, (awk->log.capa + 1) * HAWK_SIZEOF(*awk->log.ptr)); + if (!awk->log.ptr) goto oops; + awk->tree.ngbls = 0; awk->tree.ngbls_base = 0; awk->tree.begin = HAWK_NULL; @@ -263,6 +263,8 @@ oops: fini_token (&awk->ntok); fini_token (&awk->tok); fini_token (&awk->ptok); + if (awk->log.ptr) hawk_freemem (awk, awk->log.ptr); + awk->log.capa = 0; return -1; } @@ -275,6 +277,14 @@ void hawk_fini (hawk_t* awk) hawk_clear (awk); /*hawk_clrfnc (awk);*/ + if (awk->log.len > 0) + { + int shuterr = awk->shuterr; + awk->shuterr = 1; + awk->prm.logwrite (awk, awk->log.last_mask, awk->log.ptr, awk->log.len); + awk->shuterr = shuterr; + } + for (ecb = awk->ecb; ecb; ecb = ecb->next) if (ecb->close) ecb->close (awk); @@ -301,6 +311,25 @@ void hawk_fini (hawk_t* awk) { if (awk->opt.mod[i].ptr) hawk_freemem (awk, awk->opt.mod[i].ptr); } + + if (awk->log.len > 0) + { + /* flush pending log message that could be generated by the fini + * callbacks. however, the actual logging might not be produced at + * this point because one of the callbacks could arrange to stop + * logging */ + int shuterr = awk->shuterr; + awk->shuterr = 1; + awk->prm.logwrite (awk, awk->log.last_mask, awk->log.ptr, awk->log.len); + awk->shuterr = shuterr; + } + + if (awk->log.ptr) + { + hawk_freemem (awk, awk->log.ptr); + awk->log.capa = 0; + awk->log.len = 0; + } } static hawk_rbt_walk_t unload_module (hawk_rbt_t* rbt, hawk_rbt_pair_t* pair, void* ctx)