~alcinnz/rhapsode

226826fe24c2f7e96add3381635c6d707bed97a3 — Adrian Cochrane 4 years ago b214792
Expose additional logging via commandline options.
2 files changed, 28 insertions(+), 1 deletions(-)

M src/Input.hs
M src/main.c
M src/Input.hs => src/Input.hs +10 -0
@@ 202,3 202,13 @@ c_fetchURL c_session c_mimes c_referer c_uri = do
    let uri' = nullURI `fromMaybe` parseURIReference uri `relativeTo` Types.url referer
    doc <- fetchDocument session referer (words mimes) uri'
    newStablePtr doc

foreign export ccall c_enableLogging :: StablePtr Session -> IO ()

c_enableLogging c_session = deRefStablePtr c_session >>= enableLogging

foreign export ccall c_writeLog :: CString -> StablePtr Session -> IO ()

c_writeLog c_path c_session = do
    path <- peekCString c_path
    withFile path AppendMode (\logfile -> deRefStablePtr c_session >>= writeLog logfile)

M src/main.c => src/main.c +18 -1
@@ 26,6 26,9 @@ void *c_fetchURL(struct session*, char*, struct page*, char*);
//struct page **c_fetchURLs(struct session*, struct page*, char**); // FIXME segfaults.
void c_freePage(struct page*);

void c_enableLogging(struct session*);
void c_writeLog(char*, struct session*);

char *c_renderDoc(struct session*, struct page*, _Bool);
char **c_extractLinks(struct page*);
char **c_docLinksAndRendering(struct session*, struct page*, _Bool); // FIXME segfaults.


@@ 235,6 238,7 @@ int main(int argc, char **argv) {
    hs_init(&argc, &argv);

    char *mimes = "text/html text/xml application/xml application/xhtml+xml text/plain";
    char *logpath = NULL;
    FILE *fd_ssml = NULL;
    FILE *fd_links = NULL;
    int use_espeak = 0;


@@ 246,7 250,7 @@ int main(int argc, char **argv) {
    int c;
    opterr = 0;
    #ifdef WITH_SPEECHD
    while ((c = getopt(argc, argv, "xs::l::kw::dh")) != -1) {
    while ((c = getopt(argc, argv, "xs::l::L:kw::dh")) != -1) {
    #else
    while ((c = getopt(argc, argv, "xs::l::kw::h")) != -1) {
    #endif


@@ 260,6 264,9 @@ int main(int argc, char **argv) {
        case 'l':
            fd_links = parse_opt_file();
            break;
        case 'L':
            logpath = optarg;
            break;
        case 'k':
            read_keyboard = 1;
            // Read input character by character, not line by line.


@@ 289,6 296,12 @@ int main(int argc, char **argv) {
            fprintf(stderr, "\t-s\tsilent/SSML\tWrites SSML to the specified file or stdout.\n");
            fprintf(stderr, "\t\t\thttps://xkcd.com/1692/\n");
            fprintf(stderr, "\t-l\tlinks\tWrite extracted links to specifed file or stdout as TSV.\n");
            fprintf(stderr, "\t-L\tlog\tWrite (append) network request timing to specified filepath.\n");
            fprintf(stderr, "\t-k\tkeyboard\tRead arrow key navigation & links from stdin.\n");
            fprintf(stderr, "\t-w\t.wav\tWrite an audio recording of the webpage, or (DEFAULT) immediately output through speakers.\n");
            #ifdef WITH_SPEECHD
            fprintf(stderr, "\t-d\tSpeechD\tSchedule page read via the SpeechD daemon. (BROKEN)\n");
            #endif
            fprintf(stderr, "\t-h\thelp\tOutputs this usage information to stderr.\n");
            fprintf(stderr, "\t\t\tIf both -s & -l are enabled without an argument, writes to stderr instead.\n");



@@ 316,10 329,14 @@ int main(int argc, char **argv) {
        #endif
        else printf("%s\n", argv[i]);

        if (logpath != NULL) c_enableLogging(session);

        struct page *page = c_fetchURL(session, mimes, referer, argv[i]);
        ssml = c_renderDoc(session, page, use_espeak);
        char **links = c_extractLinks(page);

        if (logpath != NULL) c_writeLog(logpath, session);

        if (fd_ssml != NULL) fprintf(fd_ssml, "%s\n", ssml);
        if (fd_links != NULL) write_links(fd_links, links);
        if (use_espeak & speak_err == 0) speak(ssml, "main", NULL);