- if (logging && !logfile) {
- logfile=fopen(logfilename, "a");
- if (!logfile) {
- fprintf(stderr, "Can't open logfile, logging disabled!\n");
- logging=0;
- }
- }
-
+ // lock this section to avoid interlacing prints from different threads
+ pthread_mutex_lock(&print_lock);
+
+ if (logging && !logfile) {
+ logfile=fopen(logfilename, "a");
+ if (!logfile) {
+ fprintf(stderr, "Can't open logfile, logging disabled!\n");
+ logging=0;
+ }
+ }
+
+ // If there is an incoming message from the hardware (eg: lf hid read) in
+ // the background (while the prompt is displayed and accepting user input),
+ // stash the prompt and bring it back later.
+#ifdef RL_STATE_READCMD
+ // We are using GNU readline. libedit (OSX) doesn't support this flag.