mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-02 03:52:49 -05:00
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends and so forth are constantly calling. Previously, logevent was owned by the Frontend, which would store the message into its list for the GUI Event Log dialog (or print it to standard error, or whatever) and then pass it _back_ to LogContext to write to the currently open log file. Now it's the other way round: LogContext gets the message from the back end first, writes it to its log file if it feels so inclined, and communicates it back to the front end. This means that lots of parts of the back end system no longer need to have a pointer to a full-on Frontend; the only thing they needed it for was logging, so now they just have a LogContext (which many of them had to have anyway, e.g. for logging SSH packets or session traffic). LogContext itself also doesn't get a full Frontend pointer any more: it now talks back to the front end via a little vtable of its own called LogPolicy, which contains the method that passes Event Log entries through, the old askappend() function that decides whether to truncate a pre-existing log file, and an emergency function for printing an especially prominent message if the log file can't be created. One minor nice effect of this is that console and GUI apps can implement that last function subtly differently, so that Unix console apps can write it with a plain \n instead of the \r\n (harmless but inelegant) that the old centralised implementation generated. One other consequence of this is that the LogContext has to be provided to backend_init() so that it's available to backends from the instant of creation, rather than being provided via a separate API call a couple of function calls later, because backends have typically started doing things that need logging (like making network connections) before the call to backend_provide_logctx. Fortunately, there's no case in the whole code base where we don't already have logctx by the time we make a backend (so I don't actually remember why I ever delayed providing one). So that shortens the backend API by one function, which is always nice. While I'm tidying up, I've also moved the printf-style logeventf() and the handy logevent_and_free() into logging.c, instead of having copies of them scattered around other places. This has also let me remove some stub functions from a couple of outlying applications like Pageant. Finally, I've removed the pointless "_tag" at the end of LogContext's official struct name.
This commit is contained in:
80
putty.h
80
putty.h
@ -480,7 +480,8 @@ struct Backend {
|
||||
};
|
||||
struct BackendVtable {
|
||||
const char *(*init) (Frontend *frontend, Backend **backend_out,
|
||||
Conf *conf, const char *host, int port,
|
||||
LogContext *logctx, Conf *conf,
|
||||
const char *host, int port,
|
||||
char **realhost, int nodelay, int keepalive);
|
||||
|
||||
void (*free) (Backend *be);
|
||||
@ -501,7 +502,6 @@ struct BackendVtable {
|
||||
int (*sendok) (Backend *be);
|
||||
int (*ldisc_option_state) (Backend *be, int);
|
||||
void (*provide_ldisc) (Backend *be, Ldisc *ldisc);
|
||||
void (*provide_logctx) (Backend *be, LogContext *logctx);
|
||||
/* Tells the back end that the front end buffer is clearing. */
|
||||
void (*unthrottle) (Backend *be, int bufsize);
|
||||
int (*cfg_info) (Backend *be);
|
||||
@ -515,8 +515,8 @@ struct BackendVtable {
|
||||
int default_port;
|
||||
};
|
||||
|
||||
#define backend_init(vt, fe, out, conf, host, port, rhost, nd, ka) \
|
||||
((vt)->init(fe, out, conf, host, port, rhost, nd, ka))
|
||||
#define backend_init(vt, fe, out, logctx, conf, host, port, rhost, nd, ka) \
|
||||
((vt)->init(fe, out, logctx, conf, host, port, rhost, nd, ka))
|
||||
#define backend_free(be) ((be)->vt->free(be))
|
||||
#define backend_reconfig(be, conf) ((be)->vt->reconfig(be, conf))
|
||||
#define backend_send(be, buf, len) ((be)->vt->send(be, buf, len))
|
||||
@ -530,8 +530,6 @@ struct BackendVtable {
|
||||
#define backend_ldisc_option_state(be, opt) \
|
||||
((be)->vt->ldisc_option_state(be, opt))
|
||||
#define backend_provide_ldisc(be, ldisc) ((be)->vt->provide_ldisc(be, ldisc))
|
||||
#define backend_provide_logctx(be, logctx) \
|
||||
((be)->vt->provide_logctx(be, logctx))
|
||||
#define backend_unthrottle(be, bufsize) ((be)->vt->unthrottle(be, bufsize))
|
||||
#define backend_cfg_info(be) ((be)->vt->cfg_info(be))
|
||||
|
||||
@ -1170,14 +1168,64 @@ int format_arrow_key(char *buf, Terminal *term, int xkey, int ctrl);
|
||||
/*
|
||||
* Exports from logging.c.
|
||||
*/
|
||||
LogContext *log_init(Frontend *frontend, Conf *conf);
|
||||
struct LogPolicyVtable {
|
||||
/*
|
||||
* Pass Event Log entries on from LogContext to the front end,
|
||||
* which might write them to standard error or save them for a GUI
|
||||
* list box or other things.
|
||||
*/
|
||||
void (*eventlog)(LogPolicy *lp, const char *event);
|
||||
|
||||
/*
|
||||
* Ask what to do about the specified output log file already
|
||||
* existing. Can return four values:
|
||||
*
|
||||
* - 2 means overwrite the log file
|
||||
* - 1 means append to the log file
|
||||
* - 0 means cancel logging for this session
|
||||
* - -1 means please wait, and callback() will be called with one
|
||||
* of those options.
|
||||
*/
|
||||
int (*askappend)(LogPolicy *lp, Filename *filename,
|
||||
void (*callback)(void *ctx, int result), void *ctx);
|
||||
|
||||
/*
|
||||
* Emergency logging when the log file itself can't be opened,
|
||||
* which typically means we want to shout about it more loudly
|
||||
* than a mere Event Log entry.
|
||||
*
|
||||
* One reasonable option is to send it to the same place that
|
||||
* stderr output from the main session goes (so, either a console
|
||||
* tool's actual stderr, or a terminal window). In many cases this
|
||||
* is unlikely to cause this error message to turn up
|
||||
* embarrassingly in a log file of real server output, because the
|
||||
* whole point is that we haven't managed to open any such log
|
||||
* file :-)
|
||||
*/
|
||||
void (*logging_error)(LogPolicy *lp, const char *event);
|
||||
};
|
||||
struct LogPolicy {
|
||||
const LogPolicyVtable *vt;
|
||||
};
|
||||
#define lp_eventlog(lp, event) ((lp)->vt->eventlog(lp, event))
|
||||
#define lp_askappend(lp, fn, cb, ctx) ((lp)->vt->askappend(lp, fn, cb, ctx))
|
||||
#define lp_logging_error(lp, event) ((lp)->vt->logging_error(lp, event))
|
||||
|
||||
LogContext *log_init(LogPolicy *lp, Conf *conf);
|
||||
void log_free(LogContext *logctx);
|
||||
void log_reconfig(LogContext *logctx, Conf *conf);
|
||||
void logfopen(LogContext *logctx);
|
||||
void logfclose(LogContext *logctx);
|
||||
void logtraffic(LogContext *logctx, unsigned char c, int logmode);
|
||||
void logflush(LogContext *logctx);
|
||||
void log_eventlog(LogContext *logctx, const char *string);
|
||||
void logevent(LogContext *logctx, const char *event);
|
||||
void logeventf(LogContext *logctx, const char *fmt, ...);
|
||||
/*
|
||||
* Pass a dynamically allocated string to logevent and immediately
|
||||
* free it. Intended for use by wrapper macros which pass the return
|
||||
* value of dupprintf straight to this.
|
||||
*/
|
||||
void logevent_and_free(LogContext *logctx, char *event);
|
||||
enum { PKT_INCOMING, PKT_OUTGOING };
|
||||
enum { PKTLOG_EMIT, PKTLOG_BLANK, PKTLOG_OMIT };
|
||||
struct logblank_t {
|
||||
@ -1191,6 +1239,10 @@ void log_packet(LogContext *logctx, int direction, int type,
|
||||
const unsigned long *sequence,
|
||||
unsigned downstream_id, const char *additional_log_text);
|
||||
|
||||
/* This is defined by applications that have an obvious logging
|
||||
* destination like standard error or the GUI. */
|
||||
extern LogPolicy default_logpolicy[1];
|
||||
|
||||
/*
|
||||
* Exports from testback.c
|
||||
*/
|
||||
@ -1352,7 +1404,6 @@ int wc_unescape(char *output, const char *wildcard);
|
||||
/*
|
||||
* Exports from frontend (windlg.c etc)
|
||||
*/
|
||||
void logevent(Frontend *frontend, const char *);
|
||||
void pgp_fingerprints(void);
|
||||
/*
|
||||
* verify_ssh_host_key() can return one of three values:
|
||||
@ -1386,16 +1437,6 @@ int askalg(Frontend *frontend, const char *algtype, const char *algname,
|
||||
void (*callback)(void *ctx, int result), void *ctx);
|
||||
int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
|
||||
void (*callback)(void *ctx, int result), void *ctx);
|
||||
/*
|
||||
* askappend can return four values:
|
||||
*
|
||||
* - 2 means overwrite the log file
|
||||
* - 1 means append to the log file
|
||||
* - 0 means cancel logging for this session
|
||||
* - -1 means please wait.
|
||||
*/
|
||||
int askappend(Frontend *frontend, Filename *filename,
|
||||
void (*callback)(void *ctx, int result), void *ctx);
|
||||
|
||||
/*
|
||||
* Exports from console frontends (wincons.c, uxcons.c)
|
||||
@ -1403,7 +1444,6 @@ int askappend(Frontend *frontend, Filename *filename,
|
||||
*/
|
||||
extern int console_batch_mode;
|
||||
int console_get_userpass_input(prompts_t *p);
|
||||
void console_provide_logctx(LogContext *logctx);
|
||||
int is_interactive(void);
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user