You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9KB

  1. //! Tools to record and display what's happening in your program
  2. //!
  3. #![feature(test)]
  4. #[macro_use] extern crate slog;
  5. #[allow(unused_imports)]
  6. #[macro_use] extern crate money;
  7. extern crate test;
  8. extern crate zmq;
  9. extern crate influent;
  10. extern crate chrono;
  11. extern crate hyper;
  12. extern crate termion;
  13. extern crate sloggers;
  14. extern crate slog_term;
  15. extern crate slog_async;
  16. extern crate fnv;
  17. extern crate ordermap;
  18. extern crate decimal;
  19. extern crate uuid;
  20. extern crate hdrhistogram;
  21. extern crate smallvec;
  22. extern crate num;
  23. //extern crate windows;
  24. extern crate pubsub as pub_sub;
  25. use chrono::{DateTime, Utc};
  26. #[allow(unused_imports)]
  27. use sloggers::Build;
  28. #[allow(unused_imports)]
  29. pub use sloggers::types::Severity;
  30. use sloggers::types::TimeZone;
  31. #[allow(unused_imports)]
  32. use sloggers::file::FileLoggerBuilder;
  33. use slog::Drain;
  34. use uuid::Uuid;
  35. pub mod influx;
  36. #[cfg(feature = "warnings")]
  37. pub mod warnings;
  38. pub mod latency;
  39. pub mod hist;
  40. #[cfg(feature = "trace")]
  41. pub const LOG_LEVEL : Severity = Severity::Trace;
  42. #[cfg(all(feature = "debug", not(feature = "trace")))]
  43. pub const LOG_LEVEL : Severity = Severity::Debug;
  44. #[cfg(not(any(feature = "debug", feature = "trace")))]
  45. pub const LOG_LEVEL : Severity = Severity::Info;
  46. const CHANNEL_SIZE: usize = 20_000;
  47. /// converts a chrono::DateTime to an integer timestamp (ns)
  48. ///
  49. pub fn nanos(t: DateTime<Utc>) -> u64 {
  50. (t.timestamp() as u64) * 1_000_000_000_u64 + (t.timestamp_subsec_nanos() as u64)
  51. }
  52. //#[cfg(not(any(test, feature = "test")))]
  53. pub fn file_logger(path: &str, level: Severity) -> slog::Logger {
  54. let mut builder = FileLoggerBuilder::new(path);
  55. builder.level(level);
  56. builder.timezone(TimeZone::Utc);
  57. builder.channel_size(CHANNEL_SIZE);
  58. builder.build().unwrap()
  59. }
  60. pub fn truncating_file_logger(path: &str, level: Severity) -> slog::Logger {
  61. let mut builder = FileLoggerBuilder::new(path);
  62. builder.level(level);
  63. builder.timezone(TimeZone::Utc);
  64. builder.truncate();
  65. builder.channel_size(CHANNEL_SIZE);
  66. builder.build().unwrap()
  67. }
  68. #[deprecated(since="0.4.0", note="Turns out the file logger in sloggers uses async, \
  69. making the async here duplicative")]
  70. pub fn async_file_logger(path: &str, level: Severity) -> slog::Logger {
  71. let drain = file_logger(path, level);
  72. let async_drain =
  73. slog_async::Async::new(drain)
  74. .chan_size(100_000)
  75. .build();
  76. slog::Logger::root(async_drain.fuse(), o!())
  77. }
  78. pub fn dt_nanos(t: DateTime<Utc>) -> i64 {
  79. (t.timestamp() as i64) * 1_000_000_000_i64 + (t.timestamp_subsec_nanos() as i64)
  80. }
  81. pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
  82. (d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
  83. }
  84. pub fn short_uuid(id: &Uuid) -> String {
  85. if cfg!(feature = "disable-short-uuid") {
  86. id.to_string()
  87. } else {
  88. format!("{}", &id.to_string()[..8])
  89. }
  90. }