1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2018-2018 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #include "console_output.h"
23 
24 #include <stdarg.h>
25 
26 #include "include/bareos.h"
27 
28 static FILE* output_file_ = stdout;
29 static bool teeout_enabled_ = false;
30 
31 /**
32  * Send a line to the output file and or the terminal
33  */
ConsoleOutputFormat(const char * fmt,...)34 void ConsoleOutputFormat(const char* fmt, ...)
35 {
36   std::vector<char> buf(3000);
37   va_list arg_ptr;
38 
39   va_start(arg_ptr, fmt);
40   Bvsnprintf(buf.data(), 3000 - 1, (char*)fmt, arg_ptr);
41   va_end(arg_ptr);
42   ConsoleOutput(buf.data());
43 }
44 
ConsoleOutput(const char * buf)45 void ConsoleOutput(const char* buf)
46 {
47   fputs(buf, output_file_);
48   fflush(output_file_);
49   if (teeout_enabled_) {
50     fputs(buf, stdout);
51     fflush(stdout);
52   }
53 }
54 
EnableTeeOut()55 void EnableTeeOut() { teeout_enabled_ = true; }
56 
DisableTeeOut()57 void DisableTeeOut() { teeout_enabled_ = false; }
58 
SetTeeFile(FILE * f)59 void SetTeeFile(FILE* f) { output_file_ = f; }
60 
CloseTeeFile()61 void CloseTeeFile()
62 {
63   if (output_file_ != stdout) {
64     fclose(output_file_);
65     SetTeeFile(stdout);
66     DisableTeeOut();
67   }
68 }
69