]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
3 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // UI utilities | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include <stdarg.h> | |
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <stdbool.h> | |
16 | #include <readline/readline.h> | |
17 | #include <pthread.h> | |
18 | ||
19 | #include "ui.h" | |
20 | ||
21 | double CursorScaleFactor = 1; | |
22 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; | |
23 | int offline; | |
24 | int flushAfterWrite = 0; //buzzy | |
25 | int GridOffset = 0; | |
26 | bool GridLocked = false; | |
27 | ||
28 | extern pthread_mutex_t print_lock; | |
29 | ||
30 | static char *logfilename = "proxmark3.log"; | |
31 | ||
32 | void PrintAndLog(char *fmt, ...) | |
33 | { | |
34 | char *saved_line; | |
35 | int saved_point; | |
36 | va_list argptr, argptr2; | |
37 | static FILE *logfile = NULL; | |
38 | static int logging=1; | |
39 | ||
40 | // lock this section to avoid interlacing prints from different threads | |
41 | pthread_mutex_lock(&print_lock); | |
42 | ||
43 | if (logging && !logfile) { | |
44 | logfile=fopen(logfilename, "a"); | |
45 | if (!logfile) { | |
46 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
47 | logging=0; | |
48 | } | |
49 | } | |
50 | ||
51 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
52 | ||
53 | if (need_hack) { | |
54 | saved_point = rl_point; | |
55 | saved_line = rl_copy_text(0, rl_end); | |
56 | rl_save_prompt(); | |
57 | rl_replace_line("", 0); | |
58 | rl_redisplay(); | |
59 | } | |
60 | ||
61 | va_start(argptr, fmt); | |
62 | va_copy(argptr2, argptr); | |
63 | vprintf(fmt, argptr); | |
64 | printf(" "); // cleaning prompt | |
65 | va_end(argptr); | |
66 | printf("\n"); | |
67 | ||
68 | if (need_hack) { | |
69 | rl_restore_prompt(); | |
70 | rl_replace_line(saved_line, 0); | |
71 | rl_point = saved_point; | |
72 | rl_redisplay(); | |
73 | free(saved_line); | |
74 | } | |
75 | ||
76 | if (logging && logfile) { | |
77 | vfprintf(logfile, fmt, argptr2); | |
78 | fprintf(logfile,"\n"); | |
79 | fflush(logfile); | |
80 | } | |
81 | va_end(argptr2); | |
82 | ||
83 | if (flushAfterWrite == 1) //buzzy | |
84 | { | |
85 | fflush(NULL); | |
86 | } | |
87 | //release lock | |
88 | pthread_mutex_unlock(&print_lock); | |
89 | } | |
90 | ||
91 | ||
92 | void SetLogFilename(char *fn) | |
93 | { | |
94 | logfilename = fn; | |
95 | } |