logger.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. use std::io::Write;
  20. use std::io::stderr;
  21. use log::{Log, LogLevel, LogRecord, LogMetadata};
  22. use ansi_term::Style;
  23. use ansi_term::Colour;
  24. use ansi_term::ANSIString;
  25. pub struct ImagLogger {
  26. lvl: LogLevel,
  27. color_enabled: bool,
  28. }
  29. impl ImagLogger {
  30. pub fn new(lvl: LogLevel) -> ImagLogger {
  31. ImagLogger {
  32. lvl: lvl,
  33. color_enabled: true
  34. }
  35. }
  36. pub fn with_color(mut self, b: bool) -> ImagLogger {
  37. self.color_enabled = b;
  38. self
  39. }
  40. fn style_or_not(&self, c: Style, s: String) -> ANSIString {
  41. if self.color_enabled {
  42. c.paint(s)
  43. } else {
  44. ANSIString::from(s)
  45. }
  46. }
  47. fn color_or_not(&self, c: Colour, s: String) -> ANSIString {
  48. if self.color_enabled {
  49. c.paint(s)
  50. } else {
  51. ANSIString::from(s)
  52. }
  53. }
  54. }
  55. impl Log for ImagLogger {
  56. fn enabled(&self, metadata: &LogMetadata) -> bool {
  57. metadata.level() <= self.lvl
  58. }
  59. fn log(&self, record: &LogRecord) {
  60. use ansi_term::Colour::Red;
  61. use ansi_term::Colour::Yellow;
  62. use ansi_term::Colour::Cyan;
  63. if self.enabled(record.metadata()) {
  64. // TODO: This is just simple logging. Maybe we can enhance this lateron
  65. let loc = record.location();
  66. match record.metadata().level() {
  67. LogLevel::Debug => {
  68. let lvl = self.color_or_not(Cyan, format!("{}", record.level()));
  69. let file = self.color_or_not(Cyan, format!("{}", loc.file()));
  70. let ln = self.color_or_not(Cyan, format!("{}", loc.line()));
  71. let args = self.color_or_not(Cyan, format!("{}", record.args()));
  72. writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}", lvl, file, ln, args).ok();
  73. },
  74. LogLevel::Warn | LogLevel::Error => {
  75. let lvl = self.style_or_not(Red.blink(), format!("{}", record.level()));
  76. let args = self.color_or_not(Red, format!("{}", record.args()));
  77. writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
  78. },
  79. LogLevel::Info => {
  80. let lvl = self.color_or_not(Yellow, format!("{}", record.level()));
  81. let args = self.color_or_not(Yellow, format!("{}", record.args()));
  82. writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
  83. },
  84. _ => {
  85. writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok();
  86. },
  87. }
  88. }
  89. }
  90. }