summaryrefslogtreecommitdiffstats
path: root/Src/Console.c
blob: 03738bf96fb6d18bfbf7642fc6054a1a825e9b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Video On Demand Samples
 *
 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "Console.h"

///Our shared memory key
static int shmkey = 072162537;
//The minimum priority
static ConsolePrio_t minPrio = PRIO_LOW;

/*! \cond PRIVATE */
typedef struct
{
    ///The first process will setup the critical section and set this variable to true.
    bool initialized;
    ///If set to true, multiple processeses are synced via shared memory.
    bool processSynced;
    ///If set to true, there is an segmented print ongoing (Start, Continue, Exit).
    bool criticalSection;
    //If is in a critical segmented print, this variable will hold the prio for Start, Continue, Exit.
    ConsolePrio_t criticalSectionPrio;
    ///Handle of the shared mutex.
    pthread_mutex_t mutex;
} sharedData_t;
/*! \endcond */

/*----------------------------------------------------------*/
/*! \brief Pointer to the shared memory instance.
 */
/*----------------------------------------------------------*/
static sharedData_t *data = NULL;

void ConsoleInit( bool synchronizeProcesses )
{
    pthread_mutexattr_t attr;

    if( synchronizeProcesses )
    {
        int shmid = shmget( shmkey, sizeof( sharedData_t ), IPC_CREAT | 0666 );
        if( ( sharedData_t * )-1 == ( data = ( sharedData_t* )shmat( shmid, NULL, 0 ) ) )
        {
            data = NULL;
            fprintf( stderr, RED"ConsoleInit failed, because shared memory could not be accessed."RESETCOLOR"\n" );
            return;
        }
    }
    else
    {
        data = ( sharedData_t * )calloc( 1, sizeof( sharedData_t ) );
    }
    if( ( NULL != data ) && !data->initialized )
    {
        data->processSynced = synchronizeProcesses;
        data->initialized = true;
        data->criticalSection = false;

        pthread_mutexattr_init( &attr );
        if( synchronizeProcesses )
            pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
        else
            pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
        pthread_mutex_init( &data->mutex, &attr );
    }
}

void ConsoleDeinit( void )
{
    if( NULL != data && !data->processSynced )
    {
        free( data );
        data = NULL;
    }
}

void ConsoleSetPrio( ConsolePrio_t prio )
{
    minPrio = prio;
}

void ConsolePrintf( ConsolePrio_t prio, const char *statement, ... )
{
    int err;
    if( prio < minPrio || NULL == statement )
        return;
    if( NULL == data )
    {
        fprintf( stderr, RED"ConsolePrintf data was null"RESETCOLOR"\n" );
        return;
    }
    if( 0 != ( err = pthread_mutex_lock( &data->mutex ) ) )
    {
        fprintf( stderr, RED"ConsolePrintf, pthread_mutex_lock error: %d"RESETCOLOR"\n", err );
        return;
    }

    va_list args;
    va_start( args, statement );
    vfprintf( stderr, statement, args );
    va_end( args );

    if( 0 != ( err = pthread_mutex_unlock( &data->mutex ) ) )
    {
        fprintf( stderr, RED"ConsolePrintf, pthread_mutex_unlock error: %d"RESETCOLOR"\n", err );
        return;
    }
}

void ConsolePrintfStart( ConsolePrio_t prio, const char *statement, ... )
{
    int err;
    if( NULL == data )
    {
        fprintf( stderr, RED"ConsolePrintfStart data was null"RESETCOLOR"\n" );
        return;
    }
    if( 0 != ( err = pthread_mutex_lock( &data->mutex ) ) )
    {
        fprintf( stderr, RED"ConsolePrintfStart, pthread_mutex_lock error: %d"RESETCOLOR"\n", err );
        return;
    }
    data->criticalSection = true;
    data->criticalSectionPrio = prio;

    if( data->criticalSectionPrio >= minPrio && NULL != statement )
    {
        va_list args;
        va_start( args, statement );
        vfprintf( stderr, statement, args );
        va_end( args );
    }
}

void ConsolePrintfContinue( const char *statement, ... )
{
    if( NULL == data )
    {
        fprintf( stderr, RED"ConsolePrintfContinue data was null"RESETCOLOR"\n" );
        return;
    }
    if( !data->criticalSection )
    {
        fprintf( stderr, RED"ConsolePrintfContinue not in critical section"RESETCOLOR"\n" );
        return;
    }

    if( data->criticalSectionPrio >= minPrio && NULL != statement )
    {
        va_list args;
        va_start( args, statement );
        vfprintf( stderr, statement, args );
        va_end( args );
    }
}

void ConsolePrintfExit( const char *statement, ... )
{
    int err;
    if( NULL == data )
    {
        fprintf( stderr, RED"ConsolePrintfExit data was null"RESETCOLOR"\n" );
        return;
    }
    if( !data->criticalSection )
    {
        fprintf( stderr, RED"ConsolePrintfExit not in critical section"RESETCOLOR"\n" );
        return;
    }
    if( data->criticalSectionPrio >= minPrio && NULL != statement )
    {
        va_list args;
        va_start( args, statement );
        vfprintf( stderr, statement, args );
        va_end( args );
    }
    data->criticalSection = false;
    if( 0 != ( err = pthread_mutex_unlock( &data->mutex ) ) )
    {
        fprintf( stderr, RED"ConsolePrintfExit, pthread_mutex_unlock error: %d"RESETCOLOR"\n", err );
    }
}