move file output to a separate display-file

This commit is contained in:
François Revol 2018-10-19 02:58:38 +02:00
parent 5c389ecdb5
commit e97be17f7f
4 changed files with 170 additions and 35 deletions

View File

@ -110,6 +110,8 @@ set(conky_sources
display-output.hh
display-console.cc
display-console.hh
display-file.cc
display-file.hh
display-ncurses.cc
display-ncurses.hh
display-http.cc

View File

@ -310,15 +310,6 @@ conky::range_config_setting<int> net_avg_samples("net_avg_samples", 1, 14, 2,
conky::range_config_setting<int> diskio_avg_samples("diskio_avg_samples", 1, 14,
2, true);
/* filenames for output */
static conky::simple_config_setting<std::string> overwrite_file(
"overwrite_file", std::string(), true);
static FILE *overwrite_fpointer = nullptr;
static conky::simple_config_setting<std::string> append_file("append_file",
std::string(),
true);
static FILE *append_fpointer = nullptr;
#ifdef BUILD_X11
static conky::simple_config_setting<bool> show_graph_scale("show_graph_scale",
@ -1073,12 +1064,6 @@ static void draw_string(const char *s) {
#ifdef BUILD_X11
width_of_s = get_string_width(s);
#endif /* BUILD_X11 */
if (draw_mode == FG && (overwrite_fpointer != nullptr)) {
fprintf(overwrite_fpointer, "%s\n", s);
}
if (draw_mode == FG && (append_fpointer != nullptr)) {
fprintf(append_fpointer, "%s\n", s);
}
if (conky::active_display_outputs.size() && draw_mode == FG)
for (auto output : conky::active_display_outputs)
output->draw_string(s, width_of_s);
@ -1672,18 +1657,7 @@ static void draw_stuff() {
cimlib_render(text_start_x, text_start_y, window.width, window.height);
#endif /* BUILD_IMLIB2 */
#endif /* BUILD_X11 */
if (static_cast<unsigned int>(!overwrite_file.get(*state).empty()) != 0u) {
overwrite_fpointer = fopen(overwrite_file.get(*state).c_str(), "we");
if (overwrite_fpointer == nullptr) {
NORM_ERR("Cannot overwrite '%s'", overwrite_file.get(*state).c_str());
}
}
if (static_cast<unsigned int>(!append_file.get(*state).empty()) != 0u) {
append_fpointer = fopen(append_file.get(*state).c_str(), "ae");
if (append_fpointer == nullptr) {
NORM_ERR("Cannot append to '%s'", append_file.get(*state).c_str());
}
}
for (auto output : display_outputs()) output->begin_draw_stuff();
#ifdef BUILD_X11
llua_draw_pre_hook();
if (out_to_x.get(*state)) {
@ -1723,14 +1697,7 @@ static void draw_stuff() {
if (out_to_x.get(*state)) { xpmdb_swap_buffers(); }
#endif
#endif /* BUILD_X11 && BUILD_XDBE */
if (overwrite_fpointer != nullptr) {
fclose(overwrite_fpointer);
overwrite_fpointer = nullptr;
}
if (append_fpointer != nullptr) {
fclose(append_fpointer);
append_fpointer = nullptr;
}
for (auto output : display_outputs()) output->end_draw_stuff();
}
#ifdef BUILD_X11

106
src/display-file.cc Normal file
View File

@ -0,0 +1,106 @@
/*
*
* Conky, a system monitor, based on torsmo
*
* Please see COPYING for details
*
* Copyright (C) 2018 François Revol et al.
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2019 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include "conky.h"
#include "display-file.hh"
#include "nc.h"
#include <iostream>
#include <sstream>
#include <unordered_map>
/* filenames for output */
static conky::simple_config_setting<std::string> overwrite_file(
"overwrite_file", std::string(), true);
static FILE *overwrite_fpointer = nullptr;
static conky::simple_config_setting<std::string> append_file("append_file",
std::string(),
true);
static FILE *append_fpointer = nullptr;
namespace conky {
namespace {
conky::display_output_file file_output("file");
} // namespace
namespace priv {} // namespace priv
display_output_file::display_output_file(const std::string &name_)
: display_output_base(name_) {
// lowest priority, it's a fallback
priority = 0;
}
bool display_output_file::detect() {
if (static_cast<unsigned int>(!overwrite_file.get(*state).empty()) != 0u ||
static_cast<unsigned int>(!append_file.get(*state).empty()) != 0u) {
std::cerr << "Display output '" << name << "' enabled in config."
<< std::endl;
return true;
}
return false;
}
bool display_output_file::initialize() { return true; }
bool display_output_file::shutdown() { return true; }
void display_output_file::draw_string(const char *s, int w) {
if (overwrite_fpointer != nullptr) { fprintf(overwrite_fpointer, "%s\n", s); }
if (append_fpointer != nullptr) { fprintf(append_fpointer, "%s\n", s); }
}
void display_output_file::begin_draw_stuff() {
if (static_cast<unsigned int>(!overwrite_file.get(*state).empty()) != 0u) {
overwrite_fpointer = fopen(overwrite_file.get(*state).c_str(), "we");
if (overwrite_fpointer == nullptr) {
NORM_ERR("Cannot overwrite '%s'", overwrite_file.get(*state).c_str());
}
}
if (static_cast<unsigned int>(!append_file.get(*state).empty()) != 0u) {
append_fpointer = fopen(append_file.get(*state).c_str(), "ae");
if (append_fpointer == nullptr) {
NORM_ERR("Cannot append to '%s'", append_file.get(*state).c_str());
}
}
}
void display_output_file::end_draw_stuff() {
if (overwrite_fpointer != nullptr) {
fclose(overwrite_fpointer);
overwrite_fpointer = nullptr;
}
if (append_fpointer != nullptr) {
fclose(append_fpointer);
append_fpointer = nullptr;
}
}
} // namespace conky

60
src/display-file.hh Normal file
View File

@ -0,0 +1,60 @@
/*
*
* Conky, a system monitor, based on torsmo
*
* Please see COPYING for details
*
* Copyright (C) 2018 François Revol et al.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef DISPLAY_FILE_HH
#define DISPLAY_FILE_HH
#include <limits>
#include <string>
#include <type_traits>
#include "display-output.hh"
#include "luamm.hh"
namespace conky {
/*
* A base class for file display output.
*/
class display_output_file : public display_output_base {
public:
explicit display_output_file(const std::string &name_);
virtual ~display_output_file() {}
// check if available and enabled in settings
virtual bool detect();
// connect to DISPLAY and other stuff
virtual bool initialize();
virtual bool shutdown();
virtual void draw_string(const char *s, int w);
virtual void begin_draw_stuff();
virtual void end_draw_stuff();
// file-specific
};
} // namespace conky
#endif /* DISPLAY_FILE_HH */