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.
		
		
		
		
			
				
					81 lines
				
				2.0 KiB
			
		
		
			
		
	
	
					81 lines
				
				2.0 KiB
			| 
											5 years ago
										 | #pragma once
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | #include <cassert>
 | ||
| 
											6 years ago
										 | #include <pthread.h>
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | #include <cstdint>
 | ||
|  | #include <cstdio>
 | ||
| 
											5 years ago
										 | #include <memory>
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | #include <capnp/serialize.h>
 | ||
| 
											5 years ago
										 | #include <kj/array.h>
 | ||
|  | 
 | ||
| 
											4 years ago
										 | #include "cereal/messaging/messaging.h"
 | ||
| 
											3 years ago
										 | #include "common/util.h"
 | ||
|  | #include "common/swaglog.h"
 | ||
| 
											3 years ago
										 | #include "system/hardware/hw.h"
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | const std::string LOG_ROOT = Path::log_root();
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											6 years ago
										 | #define LOGGER_MAX_HANDLES 16
 | ||
|  | 
 | ||
| 
											4 years ago
										 | class RawFile {
 | ||
| 
											5 years ago
										 |  public:
 | ||
| 
											4 years ago
										 |   RawFile(const char* path) {
 | ||
| 
											4 years ago
										 |     file = util::safe_fopen(path, "wb");
 | ||
| 
											5 years ago
										 |     assert(file != nullptr);
 | ||
|  |   }
 | ||
| 
											4 years ago
										 |   ~RawFile() {
 | ||
| 
											4 years ago
										 |     util::safe_fflush(file);
 | ||
| 
											5 years ago
										 |     int err = fclose(file);
 | ||
|  |     assert(err == 0);
 | ||
|  |   }
 | ||
|  |   inline void write(void* data, size_t size) {
 | ||
| 
											4 years ago
										 |     int written = util::safe_fwrite(data, 1, size, file);
 | ||
|  |     assert(written == size);
 | ||
| 
											5 years ago
										 |   }
 | ||
|  |   inline void write(kj::ArrayPtr<capnp::byte> array) { write(array.begin(), array.size()); }
 | ||
|  | 
 | ||
|  |  private:
 | ||
|  |   FILE* file = nullptr;
 | ||
|  | };
 | ||
|  | 
 | ||
| 
											4 years ago
										 | typedef cereal::Sentinel::SentinelType SentinelType;
 | ||
|  | 
 | ||
| 
											6 years ago
										 | typedef struct LoggerHandle {
 | ||
|  |   pthread_mutex_t lock;
 | ||
| 
											4 years ago
										 |   SentinelType end_sentinel_type;
 | ||
|  |   int exit_signal;
 | ||
| 
											6 years ago
										 |   int refcnt;
 | ||
|  |   char segment_path[4096];
 | ||
|  |   char log_path[4096];
 | ||
|  |   char qlog_path[4096];
 | ||
| 
											5 years ago
										 |   char lock_path[4096];
 | ||
| 
											4 years ago
										 |   std::unique_ptr<RawFile> log, q_log;
 | ||
| 
											6 years ago
										 | } LoggerHandle;
 | ||
|  | 
 | ||
|  | typedef struct LoggerState {
 | ||
|  |   pthread_mutex_t lock;
 | ||
|  |   int part;
 | ||
| 
											5 years ago
										 |   kj::Array<capnp::word> init_data;
 | ||
| 
											5 years ago
										 |   std::string route_name;
 | ||
| 
											6 years ago
										 |   char log_name[64];
 | ||
|  |   bool has_qlog;
 | ||
|  | 
 | ||
|  |   LoggerHandle handles[LOGGER_MAX_HANDLES];
 | ||
|  |   LoggerHandle* cur_handle;
 | ||
|  | } LoggerState;
 | ||
|  | 
 | ||
| 
											5 years ago
										 | kj::Array<capnp::word> logger_build_init_data();
 | ||
| 
											5 years ago
										 | std::string logger_get_route_name();
 | ||
| 
											4 years ago
										 | void logger_init(LoggerState *s, bool has_qlog);
 | ||
| 
											6 years ago
										 | int logger_next(LoggerState *s, const char* root_path,
 | ||
|  |                             char* out_segment_path, size_t out_segment_path_len,
 | ||
|  |                             int* out_part);
 | ||
|  | LoggerHandle* logger_get_handle(LoggerState *s);
 | ||
| 
											4 years ago
										 | void logger_close(LoggerState *s, ExitHandler *exit_handler=nullptr);
 | ||
| 
											6 years ago
										 | void logger_log(LoggerState *s, uint8_t* data, size_t data_size, bool in_qlog);
 | ||
|  | 
 | ||
|  | void lh_log(LoggerHandle* h, uint8_t* data, size_t data_size, bool in_qlog);
 | ||
|  | void lh_close(LoggerHandle* h);
 |