1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/testback.c
Simon Tatham ad0c502cef 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.
2018-10-10 21:50:50 +01:00

181 lines
4.8 KiB
C

/*
* Copyright (c) 1999 Simon Tatham
* Copyright (c) 1999 Ben Harris
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* PuTTY test backends */
#include <stdio.h>
#include <stdlib.h>
#include "putty.h"
static const char *null_init(Frontend *, Backend **, Conf *, const char *, int,
char **, int, int);
static const char *loop_init(Frontend *, Backend **, Conf *, const char *, int,
char **, int, int);
static void null_free(Backend *);
static void loop_free(Backend *);
static void null_reconfig(Backend *, Conf *);
static int null_send(Backend *, const char *, int);
static int loop_send(Backend *, const char *, int);
static int null_sendbuffer(Backend *);
static void null_size(Backend *, int, int);
static void null_special(Backend *, SessionSpecialCode, int);
static const SessionSpecial *null_get_specials(Backend *);
static int null_connected(Backend *);
static int null_exitcode(Backend *);
static int null_sendok(Backend *);
static int null_ldisc(Backend *, int);
static void null_provide_ldisc(Backend *, Ldisc *);
static void null_unthrottle(Backend *, int);
static int null_cfg_info(Backend *);
const struct BackendVtable null_backend = {
null_init, null_free, null_reconfig, null_send, null_sendbuffer, null_size,
null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
null_ldisc, null_provide_ldisc, null_unthrottle,
null_cfg_info, NULL /* test_for_upstream */, "null", -1, 0
};
const struct BackendVtable loop_backend = {
loop_init, loop_free, null_reconfig, loop_send, null_sendbuffer, null_size,
null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
null_ldisc, null_provide_ldisc, null_unthrottle,
null_cfg_info, NULL /* test_for_upstream */, "loop", -1, 0
};
struct loop_state {
Frontend *frontend;
Backend backend;
};
static const char *null_init(Frontend *frontend, Backend **backend_handle,
Conf *conf, const char *host, int port,
char **realhost, int nodelay, int keepalive) {
*backend_handle = NULL;
return NULL;
}
static const char *loop_init(Frontend *frontend, Backend **backend_handle,
Conf *conf, const char *host, int port,
char **realhost, int nodelay, int keepalive) {
struct loop_state *st = snew(struct loop_state);
st->frontend = frontend;
*backend_handle = &st->backend;
return NULL;
}
static void null_free(Backend *be)
{
}
static void loop_free(Backend *be)
{
struct loop_state *st = container_of(be, struct loop_state, backend);
sfree(st);
}
static void null_reconfig(Backend *be, Conf *conf) {
}
static int null_send(Backend *be, const char *buf, int len) {
return 0;
}
static int loop_send(Backend *be, const char *buf, int len) {
struct loop_state *st = container_of(be, struct loop_state, backend);
return from_backend(st->frontend, 0, buf, len);
}
static int null_sendbuffer(Backend *be) {
return 0;
}
static void null_size(Backend *be, int width, int height) {
}
static void null_special(Backend *be, SessionSpecialCode code, int arg) {
}
static const SessionSpecial *null_get_specials (Backend *be) {
return NULL;
}
static int null_connected(Backend *be) {
return 0;
}
static int null_exitcode(Backend *be) {
return 0;
}
static int null_sendok(Backend *be) {
return 1;
}
static void null_unthrottle(Backend *be, int backlog) {
}
static int null_ldisc(Backend *be, int option) {
return 0;
}
static void null_provide_ldisc (Backend *be, Ldisc *ldisc) {
}
static void null_provide_logctx(Backend *be, LogContext *logctx) {
}
static int null_cfg_info(Backend *be)
{
return 0;
}
/*
* Emacs magic:
* Local Variables:
* c-file-style: "simon"
* End:
*/