Skip to content

Commit

Permalink
Add log timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
dspinellis committed Aug 15, 2023
1 parent 5458846 commit 88142ab
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/ai_cli.5
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ It can be used when Emacs key bindings are in effect.
.SH [GENERAL] SECTION OPTIONS
.PP
\fIlogfile=\fR
.RS 4
Specify a path where requests and responses will be logged.
Useful for debugging and research purposes.
.RE

.PP
\fItimestamp=\fR
.RS 4
Setting \fItimestamp\fP to \fItrue\fP will cause the log file
to include the timesamp (in ISO format) of each request or response.
.RE

.SH [OPENAI] SECTION OPTIONS
Expand Down
9 changes: 9 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ prompt_number(const char *name, const char *prompt_prefix)
return result <= 0 || result > NPROMPTS ? -1 : result - 1;
}

// Return true if the string contains the value true
bool
strtobool(const char *string)
{
return strcmp(string, "true") == 0;
}


static int
config_handler(void* user, const char* section, const char* name,
const char* value)
Expand All @@ -110,6 +118,7 @@ config_handler(void* user, const char* section, const char* name,
} while(0);

MATCH(general, logfile, safe_strdup(value));
MATCH(general, timestamp, strtobool(value));
MATCH(openai, endpoint, safe_strdup(value));
MATCH(openai, key, safe_strdup(value));
MATCH(openai, model, safe_strdup(value));
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct uaprompt {

typedef struct {
const char *general_logfile; // File to log requests and responses
bool general_timestamp; // Timestamp log entries
const char *openai_endpoint; // API endpoint URL
const char *openai_key; // API key
const char *openai_model; // Model to use (e.g. gpt-3.5)
Expand Down
17 changes: 13 additions & 4 deletions src/openai_fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ initialize(config_t *config)
return curl ? 0 : -1;
}

// Write the specified string to the logfile, if enabled
void
write_log(config_t *config, const char *message)
{
if (!logfile)
return;
if (config->general_timestamp)
timestamp(logfile);
fputs(message, logfile);
}


/*
* Fetch response from the OpenAI API given the provided prompt.
Expand Down Expand Up @@ -157,8 +168,7 @@ openai_fetch(config_t *config, const char *prompt, int history_length)
" {\"role\": \"user\", \"content\": %s}\n", json_escape(prompt));
string_append(&json_request, " ]\n}\n");

if (logfile)
fputs(json_request.ptr, logfile);
write_log(config, json_request.ptr);

curl_easy_setopt(curl, CURLOPT_URL, config->openai_endpoint);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
Expand All @@ -175,8 +185,7 @@ openai_fetch(config_t *config, const char *prompt, int history_length)
return NULL;
}

if (logfile)
fputs(json_response.ptr, logfile);
write_log(config, json_response.ptr);

char *text_response = get_response_content(json_response.ptr);
free(json_request.ptr);
Expand Down
21 changes: 19 additions & 2 deletions src/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
#define _GNU_SOURCE

#include <errno.h>
#include <jansson.h>
#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <readline/readline.h>
#include <jansson.h>
#include <sys/time.h>
#include <time.h>

#include "support.h"

Expand Down Expand Up @@ -213,3 +215,18 @@ json_escape(const char *s)
result = json_dumps(string, JSON_ENCODE_ANY);
return result;
}

// Output an ISO timestamp (with microseconds) to the specified file
void
timestamp(FILE *f)
{
struct timeval tv;
char buffer[30];
struct tm *tm_info;

gettimeofday(&tv, NULL);
tm_info = localtime(&tv.tv_sec);

strftime(buffer, 26, "%Y-%m-%dT%H:%M:%S", tm_info);
fprintf(f, "{ \"timestamp\": \"%s.%06ld\" }\n", buffer, (long)tv.tv_usec);
}
3 changes: 3 additions & 0 deletions src/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* limitations under the License.
*/

#include <stdio.h>

// Used for declarations that should be static but aren't for unit testing
#define STATIC

Expand All @@ -45,3 +47,4 @@ void string_init(string_t *s, const char *value);
size_t string_write(void *data, size_t size, size_t nmemb, string_t *s);
size_t string_append(string_t *s, const char *data);
int string_appendf(string_t *s, const char *fmt, ...);
void timestamp(FILE *f);

0 comments on commit 88142ab

Please sign in to comment.