• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 14498147191

16 Apr 2025 04:51PM UTC coverage: 55.933%. Remained the same
14498147191

Pull #1877

github

web-flow
Merge 04208ccdb into 0e62e076c
Pull Request #1877: Resolved comments being parsed inside strings

12322 of 22030 relevant lines covered (55.93%)

76302.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/trick_source/sim_services/Message/MessagePublisher.cpp
1

2
#include <iostream>
3
#include <sstream>
4
#include <stdio.h>
5
#include <stdarg.h>
6
#include <time.h>
7
#include <sys/time.h>
8
#include <math.h>
9
#include <unistd.h>
10

11
#include "trick/MessagePublisher.hh"
12
#include "trick/message_proto.h"
13
#include "trick/exec_proto.h"
14

15
#define MAX_MSG_HEADER_SIZE 256
16

17
Trick::MessagePublisher * the_message_publisher ;
18

19
Trick::MessagePublisher::MessagePublisher() {
271✔
20

21
    sim_name = " " ;
271✔
22
    the_message_publisher = this ;
271✔
23

24
    tics_per_sec = 1000000 ;
271✔
25
    set_print_format() ;
271✔
26

27
}
271✔
28

29
Trick::MessagePublisher::~MessagePublisher() {
271✔
30
    the_message_publisher = NULL;
271✔
31
}
271✔
32

33
void Trick::MessagePublisher::set_print_format() {
448✔
34
    num_digits = (int)round(log10((double)tics_per_sec)) ;
448✔
35
    // use %06lu for tv_usec 
36
    snprintf(print_format, sizeof(print_format), "|L %%3d|%%s.%%06lu|%%s|%%s|T %%d|%%lld.%%0%dlld| ", num_digits) ;
448✔
37
}
448✔
38

39
int Trick::MessagePublisher::init() {
177✔
40
    tics_per_sec = exec_get_time_tic_value() ;
177✔
41
    set_print_format() ;
177✔
42
    return 0 ;
177✔
43
}
44

45
int Trick::MessagePublisher::publish(int level , std::string message) {
1,955✔
46

47
    /** @par Design Details: */
48
    std::list<Trick::MessageSubscriber *>::iterator p ;
1,955✔
49

50
    char date_buf[MAX_MSG_HEADER_SIZE];
51
    char header_buf[MAX_MSG_HEADER_SIZE];
52
    char hostname[64];
53
    time_t date ;
54
    // timeval contains both tv_sec and tv_usec
55
    // tv_sec represents seconds since the epoch and is used for time stamp without sub-second.
56
    // tv_usec are microseconds past the last second and is used for printing out sub-second.
57
    struct timeval time_val;
58
    std::string header ;
1,955✔
59
    long long tics = exec_get_time_tics() ;
1,955✔
60

61
    /** @li Create message header with level, date, host, sim name, process id, sim time. */
62
    gettimeofday(&time_val, NULL);
1,955✔
63
    
64
    // tv_sec represents seconds since the epoch
65
    date = time_val.tv_sec;
1,955✔
66
    
67
    strftime(date_buf, (size_t) 20, "%Y/%m/%d,%H:%M:%S", localtime(&date));
1,955✔
68
    (void) gethostname(hostname, (size_t) 48);
1,955✔
69
    // print_format has %lu for tv_usec, cast to unsigned long to avoid potential warning
70
    snprintf(header_buf, sizeof(header_buf), print_format , level, date_buf, (unsigned long)time_val.tv_usec, hostname,
1,955✔
71
            sim_name.c_str(), exec_get_process_id(), tics/tics_per_sec ,
1,955✔
72
            (long long)((double)(tics % tics_per_sec) * (double)(pow(10 , num_digits)/tics_per_sec)) ) ;
1,955✔
73
    header = header_buf ;
1,955✔
74

75
    /** @li Go through all its subscribers and send a message update to the subscriber that is enabled. */
76
    if ( ! subscribers.empty() ) {
1,955✔
77
        for ( p = subscribers.begin() ; p != subscribers.end() ; ++p ) {
11,671✔
78
            if ( (*p)->enabled ) {
9,725✔
79
                (*p)->update(level , header , message) ;
9,724✔
80
            }
81
        }
82
    } else {
83
        // If there are no subscribers, that probably means things have not been inited yet... just print message only
84

85
        // Building the final string in a temporary stream ensures an automic call to cout, which prevents
86
        // multithreaded sims from interleaving header and message elements.
87
        std::ostringstream oss;
10✔
88
        oss << header << message ;
10✔
89
        std::cout << oss.str() << std::flush ; } return(0) ;
3,919✔
90

91
}
92

93
Trick::MessageSubscriber * Trick::MessagePublisher::getSubscriber( std::string sub_name ) {
×
94
    std::list<Trick::MessageSubscriber *>::iterator lit ;
×
95
    for ( lit = subscribers.begin() ; lit != subscribers.end() ; ++lit ) {
×
96
        if ( ! (*lit)->name.compare(sub_name) ) {
×
97
            return *lit ;
×
98
        }
99
    }
100
    return NULL ;
×
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc