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.
		
		
		
		
			
				
					76 lines
				
				2.1 KiB
			
		
		
			
		
	
	
					76 lines
				
				2.1 KiB
			| 
											5 years ago
										 | #include <systemd/sd-journal.h>
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include <cassert>
 | ||
| 
											5 years ago
										 | #include <csignal>
 | ||
| 
											5 years ago
										 | #include <map>
 | ||
| 
											5 years ago
										 | #include <string>
 | ||
| 
											5 years ago
										 | 
 | ||
|  | #include "json11.hpp"
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include "cereal/messaging/messaging.h"
 | ||
| 
											3 years ago
										 | #include "common/timing.h"
 | ||
|  | #include "common/util.h"
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | ExitHandler do_exit;
 | ||
| 
											5 years ago
										 | int main(int argc, char *argv[]) {
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |   PubMaster pm({"androidLog"});
 | ||
|  | 
 | ||
|  |   sd_journal *journal;
 | ||
| 
											5 years ago
										 |   int err = sd_journal_open(&journal, 0);
 | ||
|  |   assert(err >= 0);
 | ||
|  |   err = sd_journal_get_fd(journal); // needed so sd_journal_wait() works properly if files rotate
 | ||
|  |   assert(err >= 0);
 | ||
|  |   err = sd_journal_seek_tail(journal);
 | ||
|  |   assert(err >= 0);
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   // workaround for bug https://github.com/systemd/systemd/issues/9934
 | ||
|  |   // call sd_journal_previous_skip after sd_journal_seek_tail (like journalctl -f does) to makes things work.
 | ||
|  |   sd_journal_previous_skip(journal, 1);
 | ||
|  | 
 | ||
| 
											5 years ago
										 |   while (!do_exit) {
 | ||
| 
											5 years ago
										 |     err = sd_journal_next(journal);
 | ||
|  |     assert(err >= 0);
 | ||
| 
											5 years ago
										 | 
 | ||
|  |     // Wait for new message if we didn't receive anything
 | ||
| 
											4 years ago
										 |     if (err == 0) {
 | ||
| 
											5 years ago
										 |       err = sd_journal_wait(journal, 1000 * 1000);
 | ||
|  |       assert (err >= 0);
 | ||
| 
											5 years ago
										 |       continue; // Try again
 | ||
| 
											5 years ago
										 |     }
 | ||
|  | 
 | ||
|  |     uint64_t timestamp = 0;
 | ||
| 
											5 years ago
										 |     err = sd_journal_get_realtime_usec(journal, ×tamp);
 | ||
|  |     assert(err >= 0);
 | ||
| 
											5 years ago
										 | 
 | ||
|  |     const void *data;
 | ||
|  |     size_t length;
 | ||
|  |     std::map<std::string, std::string> kv;
 | ||
|  | 
 | ||
| 
											4 years ago
										 |     SD_JOURNAL_FOREACH_DATA(journal, data, length) {
 | ||
| 
											5 years ago
										 |       std::string str((char*)data, length);
 | ||
|  | 
 | ||
|  |       // Split "KEY=VALUE"" on "=" and put in map
 | ||
|  |       std::size_t found = str.find("=");
 | ||
| 
											4 years ago
										 |       if (found != std::string::npos) {
 | ||
| 
											5 years ago
										 |         kv[str.substr(0, found)] = str.substr(found + 1, std::string::npos);
 | ||
|  |       }
 | ||
|  |     }
 | ||
|  | 
 | ||
| 
											5 years ago
										 |     MessageBuilder msg;
 | ||
| 
											5 years ago
										 | 
 | ||
|  |     // Build message
 | ||
| 
											5 years ago
										 |     auto androidEntry = msg.initEvent().initAndroidLog();
 | ||
| 
											5 years ago
										 |     androidEntry.setTs(timestamp);
 | ||
|  |     androidEntry.setMessage(json11::Json(kv).dump());
 | ||
|  |     if (kv.count("_PID")) androidEntry.setPid(std::atoi(kv["_PID"].c_str()));
 | ||
|  |     if (kv.count("PRIORITY")) androidEntry.setPriority(std::atoi(kv["PRIORITY"].c_str()));
 | ||
|  |     if (kv.count("SYSLOG_IDENTIFIER")) androidEntry.setTag(kv["SYSLOG_IDENTIFIER"]);
 | ||
|  | 
 | ||
|  |     pm.send("androidLog", msg);
 | ||
|  |   }
 | ||
|  | 
 | ||
|  |   sd_journal_close(journal);
 | ||
|  |   return 0;
 | ||
|  | }
 |