1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
13 #ifndef EXTERNAL_PRINTANDLOG
18 #include <readline/readline.h>
25 double CursorScaleFactor
= 1;
26 int PlotGridX
=0, PlotGridY
=0, PlotGridXdefault
= 64, PlotGridYdefault
= 64, CursorCPos
= 0, CursorDPos
= 0;
27 bool flushAfterWrite
= false; //buzzy
29 bool GridLocked
= false;
30 bool showDemod
= true;
32 static char *logfilename
= "proxmark3.log";
34 #ifndef EXTERNAL_PRINTANDLOG
35 static pthread_mutex_t print_lock
= PTHREAD_MUTEX_INITIALIZER
;
37 void PrintAndLogEx(logLevel_t level
, char *fmt
, ...) {
39 // skip debug messages if client debugging is turned off i.e. 'DATA SETDEBUG 0'
40 // if (g_debugMode == 0 && level == DEBUG)
43 char buffer
[MAX_PRINT_BUFFER
] = {0};
44 char buffer2
[MAX_PRINT_BUFFER
] = {0};
45 char prefix
[20] = {0};
48 // {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG}
49 static char *prefixes
[7] = { "", "", "INFO: ", "FAILED: ", "WARNING: ", "ERROR: ", "#: "};
53 strncpy(prefix
,_RED_(FAILED
: ), sizeof(prefix
)-1);
56 strncpy(prefix
,_BLUE_(#: ), sizeof(prefix)-1);
59 strncpy(prefix
,_GREEN_( ), sizeof(prefix
)-1);
62 strncpy(prefix
,_CYAN_(WARNING
: ), sizeof(prefix
)-1);
65 strncpy(prefix
, prefixes
[level
], sizeof(prefix
)-1);
71 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
74 // no prefixes for normal
75 if ( level
== NORMAL
) {
80 if (strchr(buffer
, '\n')) {
82 const char delim
[2] = "\n";
84 // line starts with newline
85 if (buffer
[0] == '\n')
88 token
= strtok(buffer
, delim
);
90 while (token
!= NULL
) {
92 size
= strlen(buffer2
);
95 snprintf(buffer2
+size
, sizeof(buffer2
)-size
, "%s%s\n", prefix
, token
);
97 snprintf(buffer2
+size
, sizeof(buffer2
)-size
, "\n");
99 token
= strtok(NULL
, delim
);
101 PrintAndLog(buffer2
);
103 snprintf(buffer2
, sizeof(buffer2
), "%s%.*s", prefix
, MAX_PRINT_BUFFER
- 20, buffer
);
104 PrintAndLog(buffer2
);
108 void PrintAndLog(char *fmt
, ...)
112 va_list argptr
, argptr2
;
113 static FILE *logfile
= NULL
;
114 static int logging
=1;
116 // lock this section to avoid interlacing prints from different threads
117 pthread_mutex_lock(&print_lock
);
119 if (logging
&& !logfile
) {
120 logfile
=fopen(logfilename
, "a");
122 fprintf(stderr
, "Can't open logfile, logging disabled!\n");
127 // If there is an incoming message from the hardware (eg: lf hid read) in
128 // the background (while the prompt is displayed and accepting user input),
129 // stash the prompt and bring it back later.
130 #ifdef RL_STATE_READCMD
131 // We are using GNU readline. libedit (OSX) doesn't support this flag.
132 int need_hack
= (rl_readline_state
& RL_STATE_READCMD
) > 0;
135 saved_point
= rl_point
;
136 saved_line
= rl_copy_text(0, rl_end
);
138 rl_replace_line("", 0);
143 va_start(argptr
, fmt
);
144 va_copy(argptr2
, argptr
);
145 vprintf(fmt
, argptr
);
146 printf(" "); // cleaning prompt
150 #ifdef RL_STATE_READCMD
151 // We are using GNU readline. libedit (OSX) doesn't support this flag.
154 rl_replace_line(saved_line
, 0);
155 rl_point
= saved_point
;
161 if (logging
&& logfile
) {
162 vfprintf(logfile
, fmt
, argptr2
);
163 fprintf(logfile
,"\n");
168 if (flushAfterWrite
) //buzzy
173 pthread_mutex_unlock(&print_lock
);
177 void SetLogFilename(char *fn
)
182 void SetFlushAfterWrite(bool flush_after_write
) {
183 flushAfterWrite
= flush_after_write
;