1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2024 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::{
    env, fs,
    io::Write,
    path::Path,
    str,
    sync::{Arc, Mutex},
    time::Instant,
};

use simplelog::ConfigBuilder;

use crate::Result;

/*
#[derive(Clone, Default)]
pub struct Config<T> {
    config: PhantomData<T>,
}

impl<T: Serialize + DeserializeOwned> Config<T> {
    pub fn load(path: PathBuf) -> Result<T> {
        if Path::new(&path).exists() {
            let toml = fs::read(&path)?;
            let str_buff = str::from_utf8(&toml)?;
            let config: T = toml::from_str(str_buff)?;
            Ok(config)
        } else {
            let path = path.to_str();
            if path.is_some() {
                println!("Could not find/parse configuration file in: {}", path.unwrap());
            } else {
                println!("Could not find/parse configuration file");
            }
            println!("Please follow the instructions in the README");
            Err(Error::ConfigNotFound)
        }
    }
}
*/

pub fn spawn_config(path: &Path, contents: &[u8]) -> Result<()> {
    if !path.exists() {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let mut file = fs::File::create(path)?;
        file.write_all(contents)?;
        println!("Config file created in {:?}. Please review it and try again.", path);
        std::process::exit(2);
    }

    Ok(())
}

pub fn get_log_level(verbosity_level: u8) -> simplelog::LevelFilter {
    match verbosity_level {
        0 => simplelog::LevelFilter::Info,
        1 => simplelog::LevelFilter::Info,
        2 => simplelog::LevelFilter::Debug,
        _ => simplelog::LevelFilter::Trace,
    }
}

pub fn get_log_config(verbosity_level: u8) -> simplelog::Config {
    match env::var("LOG_TARGETS") {
        Ok(x) => {
            let targets: Vec<String> = x.split(',').map(|x| x.to_string()).collect();
            let mut cfgbuilder = ConfigBuilder::new();
            match verbosity_level {
                0 => cfgbuilder.set_target_level(simplelog::LevelFilter::Debug),
                _ => cfgbuilder.set_target_level(simplelog::LevelFilter::Error),
            };

            for i in targets {
                if i.starts_with('!') {
                    cfgbuilder.add_filter_ignore(i.trim_start_matches('!').to_string());
                } else {
                    cfgbuilder.add_filter_allow(i);
                }
            }

            cfgbuilder.build()
        }
        Err(_) => {
            let mut cfgbuilder = ConfigBuilder::new();
            match verbosity_level {
                0 => cfgbuilder.set_target_level(simplelog::LevelFilter::Debug),
                _ => cfgbuilder.set_target_level(simplelog::LevelFilter::Error),
            };
            cfgbuilder.build()
        }
    }
}

/// This macro is used for a standard way of daemonizing darkfi binaries
/// with TOML config file configuration, and argument parsing. It also
/// spawns a multithreaded async executor and passes it into the given
/// function.
///
/// The Cargo.toml dependencies needed for this are:
/// ```text
/// darkfi = { path = "../../", features = ["util"] }
/// easy-parallel = "3.2.0"
/// signal-hook-async-std = "0.2.2"
/// signal-hook = "0.3.15"
/// simplelog = "0.12.0"
/// smol = "1.2.5"
///
/// # Argument parsing
/// serde = {version = "1.0.135", features = ["derive"]}
/// structopt = "0.3.26"
/// structopt-toml = "0.5.1"
/// ```
///
/// Example usage:
/// ```
/// use darkfi::{async_daemonize, cli_desc, Result};
/// use smol::stream::StreamExt;
/// use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
///
/// const CONFIG_FILE: &str = "daemond_config.toml";
/// const CONFIG_FILE_CONTENTS: &str = include_str!("../daemond_config.toml");
///
/// #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
/// #[serde(default)]
/// #[structopt(name = "daemond", about = cli_desc!())]
/// struct Args {
///     #[structopt(short, long)]
///     /// Configuration file to use
///     config: Option<String>,
///
///     #[structopt(short, long)]
///     /// Set log file to ouput into
///     log: Option<String>,
///
///     #[structopt(short, parse(from_occurrences))]
///     /// Increase verbosity (-vvv supported)
///     verbose: u8,
/// }
///
/// async_daemonize!(realmain);
/// async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
///     println!("Hello, world!");
///     Ok(())
/// }
/// ```
#[cfg(feature = "async-daemonize")]
#[macro_export]
macro_rules! async_daemonize {
    ($realmain:ident) => {
        fn main() -> Result<()> {
            let args = Args::from_args_with_toml("").unwrap();
            let cfg_path = darkfi::util::path::get_config_path(args.config, CONFIG_FILE)?;
            darkfi::util::cli::spawn_config(&cfg_path, CONFIG_FILE_CONTENTS.as_bytes())?;
            let args = Args::from_args_with_toml(&std::fs::read_to_string(cfg_path)?).unwrap();

            let log_level = darkfi::util::cli::get_log_level(args.verbose);
            let log_config = darkfi::util::cli::get_log_config(args.verbose);

            // Setup terminal logger
            let term_logger = simplelog::TermLogger::new(
                log_level,
                log_config.clone(),
                simplelog::TerminalMode::Mixed,
                simplelog::ColorChoice::Auto,
            );

            // If a log file has been configured, also create a write logger.
            // Otherwise, output to terminal logger only.
            match args.log {
                Some(ref log_path) => {
                    let log_path = darkfi::util::path::expand_path(log_path)?;
                    let log_file = std::fs::File::create(log_path)?;
                    let write_logger = simplelog::WriteLogger::new(log_level, log_config, log_file);
                    simplelog::CombinedLogger::init(vec![term_logger, write_logger])?;
                }
                None => {
                    simplelog::CombinedLogger::init(vec![term_logger])?;
                }
            }

            // https://docs.rs/smol/latest/smol/struct.Executor.html#examples
            let n_threads = std::thread::available_parallelism().unwrap().get();
            let ex = std::sync::Arc::new(smol::Executor::new());
            let (signal, shutdown) = smol::channel::unbounded::<()>();
            let (_, result) = easy_parallel::Parallel::new()
                // Run four executor threads
                .each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
                // Run the main future on the current thread.
                .finish(|| {
                    smol::future::block_on(async {
                        $realmain(args, ex.clone()).await?;
                        drop(signal);
                        Ok::<(), darkfi::Error>(())
                    })
                });

            result
        }

        /// Auxiliary structure used to keep track of signals
        struct SignalHandler {
            /// Termination signal channel receiver
            term_rx: smol::channel::Receiver<()>,
            /// Signals handle
            handle: signal_hook_async_std::Handle,
            /// SIGHUP publisher to retrieve new configuration,
            sighup_pub: darkfi::system::PublisherPtr<Args>,
        }

        impl SignalHandler {
            fn new(
                ex: std::sync::Arc<smol::Executor<'static>>,
            ) -> Result<(Self, smol::Task<Result<()>>)> {
                let (term_tx, term_rx) = smol::channel::bounded::<()>(1);
                let signals = signal_hook_async_std::Signals::new([
                    signal_hook::consts::SIGHUP,
                    signal_hook::consts::SIGTERM,
                    signal_hook::consts::SIGINT,
                    signal_hook::consts::SIGQUIT,
                ])?;
                let handle = signals.handle();
                let sighup_pub = darkfi::system::Publisher::new();
                let signals_task = ex.spawn(handle_signals(signals, term_tx, sighup_pub.clone()));

                Ok((Self { term_rx, handle, sighup_pub }, signals_task))
            }

            /// Handler waits for termination signal
            async fn wait_termination(&self, signals_task: smol::Task<Result<()>>) -> Result<()> {
                self.term_rx.recv().await?;
                print!("\r");
                self.handle.close();
                signals_task.await?;

                Ok(())
            }
        }

        /// Auxiliary task to handle SIGHUP, SIGTERM, SIGINT and SIGQUIT signals
        async fn handle_signals(
            mut signals: signal_hook_async_std::Signals,
            term_tx: smol::channel::Sender<()>,
            publisher: darkfi::system::PublisherPtr<Args>,
        ) -> Result<()> {
            while let Some(signal) = signals.next().await {
                match signal {
                    signal_hook::consts::SIGHUP => {
                        let args = Args::from_args_with_toml("").unwrap();
                        let cfg_path =
                            darkfi::util::path::get_config_path(args.config, CONFIG_FILE)?;
                        darkfi::util::cli::spawn_config(
                            &cfg_path,
                            CONFIG_FILE_CONTENTS.as_bytes(),
                        )?;
                        let args = Args::from_args_with_toml(&std::fs::read_to_string(cfg_path)?);
                        if args.is_err() {
                            println!("handle_signals():: Error parsing the config file");
                            continue
                        }
                        publisher.notify(args.unwrap()).await;
                    }
                    signal_hook::consts::SIGTERM |
                    signal_hook::consts::SIGINT |
                    signal_hook::consts::SIGQUIT => {
                        term_tx.send(()).await?;
                    }

                    _ => println!("handle_signals():: Unsupported signal"),
                }
            }
            Ok(())
        }
    };
}

pub fn fg_red(message: &str) -> String {
    format!("\x1b[31m{}\x1b[0m", message)
}

pub fn fg_green(message: &str) -> String {
    format!("\x1b[32m{}\x1b[0m", message)
}

pub fn fg_reset() -> String {
    "\x1b[0m".to_string()
}

pub struct ProgressInc {
    position: Arc<Mutex<u64>>,
    timer: Arc<Mutex<Option<Instant>>>,
}

impl Default for ProgressInc {
    fn default() -> Self {
        Self::new()
    }
}

impl ProgressInc {
    pub fn new() -> Self {
        eprint!("\x1b[?25l");
        Self { position: Arc::new(Mutex::new(0)), timer: Arc::new(Mutex::new(None)) }
    }

    pub fn inc(&self, n: u64) {
        let mut position = self.position.lock().unwrap();

        if *position == 0 {
            *self.timer.lock().unwrap() = Some(Instant::now());
        }

        *position += n;

        let binding = self.timer.lock().unwrap();
        let Some(elapsed) = binding.as_ref() else { return };
        let elapsed = elapsed.elapsed();
        let pos = *position;

        eprint!("\r[{elapsed:?}] {pos} attempts");
    }

    pub fn position(&self) -> u64 {
        *self.position.lock().unwrap()
    }

    pub fn finish_and_clear(&self) {
        *self.timer.lock().unwrap() = None;
        eprint!("\r\x1b[2K\x1b[?25h");
    }
}