Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : *
5 : : * @brief Print warnings and errors captured in the TEMP remember_history table.
6 : : *
7 : : * @details The table is created in db_init(). Output is produced only when
8 : : * delayed remembered output is enabled. If the database is unavailable
9 : : * or no rows are present, the function returns without output
10 : : *
11 : : * @return SUCCESS on completion, FAILURE if a SQLite operation fails.
12 : : *
13 : : */
14 : 455 : Return show_remembered_messages(void)
15 : : {
16 : : // Show only when the final remembered output is enabled
17 [ + + ]: 455 : if(config->show_remembered_messages_at_exit == false)
18 : : {
19 : : // Do nothing
20 : 378 : provide(SUCCESS);
21 : : }
22 : :
23 : : // DB not initialized yet (early failure): nothing to show
24 [ - + ]: 77 : if(config->db == NULL)
25 : : {
26 : 0 : provide(SUCCESS);
27 : : }
28 : :
29 : 77 : sqlite3_stmt *stmt = NULL;
30 : 77 : const char *select_sql =
31 : : "SELECT message FROM temp.remember_history ORDER BY id;";
32 : :
33 : 77 : int rc = sqlite3_prepare_v2(config->db,select_sql,-1,&stmt,NULL);
34 : :
35 [ - + ]: 77 : if(rc != SQLITE_OK)
36 : : {
37 : 0 : log_sqlite_error(config->db,rc,NULL,
38 : : "Failed to prepare remember_history query");
39 : 0 : provide(FAILURE);
40 : : }
41 : :
42 : 77 : bool printed_header = false;
43 : :
44 [ + + ]: 118 : while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
45 : : {
46 [ + + ]: 41 : if(!printed_header)
47 : : {
48 : : // Print the header only when at least one row exists.
49 : 17 : slog(EVERY,BOLD "Warnings and errors encountered:" RESET "\n");
50 : 17 : printed_header = true;
51 : : }
52 : :
53 : 41 : const unsigned char *text = sqlite3_column_text(stmt,0);
54 : 41 : int text_len = sqlite3_column_bytes(stmt,0);
55 : :
56 [ + - + - ]: 41 : if(text != NULL && text_len > 0)
57 : : {
58 : : // Use length-bounded output; remember_history messages are not
59 : : // guaranteed to be null-terminated.
60 [ + - ]: 41 : if(text[text_len - 1] == '\n')
61 : : {
62 : 41 : slog(EVERY|UNDECOR,"%.*s",text_len,text);
63 : : } else {
64 : 0 : slog(EVERY|UNDECOR,"%.*s\n",text_len,text);
65 : : }
66 : : }
67 : : }
68 : :
69 [ - + ]: 77 : if(rc != SQLITE_DONE)
70 : : {
71 : 0 : log_sqlite_error(config->db,rc,NULL,
72 : : "Failed to read remember_history");
73 : 0 : sqlite3_finalize(stmt);
74 : 0 : provide(FAILURE);
75 : : }
76 : :
77 : 77 : sqlite3_finalize(stmt);
78 : :
79 : 77 : provide(SUCCESS);
80 : : }
|