Interface Documentation
Version: invalid
packet.hh
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
16 #include <flecsi-config.h>
17 
18 #if defined(FLECSI_ENABLE_FLOG)
19 
20 #if defined(FLOG_ENABLE_MPI)
21 #include <mpi.h>
22 #endif
23 
24 #if defined(_MSC_VER)
25 #error "Need implementation for Windows"
26 #endif
27 
28 #include <sys/time.h>
29 #include <unistd.h>
30 
31 #include <algorithm>
32 #include <array>
33 #include <cstring>
34 #include <iostream>
35 #include <mutex>
36 #include <sstream>
37 #include <thread>
38 #include <vector>
39 
40 #ifndef FLOG_MAX_MESSAGE_SIZE
41 #define FLOG_MAX_MESSAGE_SIZE 1024
42 #endif
43 
44 #ifndef FLOG_MAX_PACKET_BUFFER
45 #define FLOG_MAX_PACKET_BUFFER 1024
46 #endif
47 
48 // Microsecond interval
49 #ifndef FLOG_PACKET_FLUSH_INTERVAL
50 #define FLOG_PACKET_FLUSH_INTERVAL 100000
51 #endif
52 
53 namespace flecsi {
54 namespace log {
55 
60 struct packet_t {
61  static constexpr size_t sec_bytes = sizeof(time_t);
62  static constexpr size_t usec_bytes = sizeof(suseconds_t);
63  static constexpr size_t packet_bytes =
64  sec_bytes + usec_bytes + FLOG_MAX_MESSAGE_SIZE;
65 
66  packet_t(const char * msg = nullptr) {
67  timeval stamp;
68  if(gettimeofday(&stamp, NULL)) {
69  std::cerr << "FLOG: call to gettimeofday failed!!! " << __FILE__
70  << __LINE__ << std::endl;
71  std::exit(1);
72  } // if
73 
74  strncpy(
75  bytes_.data(), reinterpret_cast<const char *>(&stamp.tv_sec), sec_bytes);
76 
77  strncpy(bytes_.data() + sec_bytes,
78  reinterpret_cast<const char *>(&stamp.tv_usec),
79  usec_bytes);
80 
81  std::ostringstream oss;
82  oss << msg;
83 
84  strcpy(bytes_.data() + sec_bytes + usec_bytes, oss.str().c_str());
85  } // packet_t
86 
87  time_t const & seconds() const {
88  return *reinterpret_cast<time_t const *>(bytes_.data());
89  } // seconds
90 
91  suseconds_t const & useconds() const {
92  return *reinterpret_cast<suseconds_t const *>(bytes_.data() + sec_bytes);
93  } // seconds
94 
95  const char * message() {
96  return bytes_.data() + sec_bytes + usec_bytes;
97  } // message
98 
99  const char * data() const {
100  return bytes_.data();
101  } // data
102 
103  static constexpr size_t bytes() {
104  return sec_bytes + usec_bytes + FLOG_MAX_MESSAGE_SIZE;
105  } // bytes
106 
107  bool operator<(packet_t const & b) {
108  return this->seconds() == b.seconds() ? this->useconds() < b.useconds()
109  : this->seconds() < b.seconds();
110  } // operator <
111 
112 private:
113  std::array<char, packet_bytes> bytes_;
114 
115 }; // packet_t
116 
117 } // namespace log
118 } // namespace flecsi
119 
120 #endif // FLECSI_ENABLE_FLOG
Definition: packet.hh:60
Definition: control.hh:31