Merge "Set proper locale to harfbuzz" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / common / kernel-trace.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "kernel-trace.h"
20
21 // EXTERNAL HEADERS
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <unistd.h>
25
26 // INTERNAL HEADERS
27 #include <dali/integration-api/debug.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40 const char* TRACE_MARKER_FILE = "/sys/kernel/debug/tracing/trace_marker";
41 const char* SPI_PREFIX = "SPI_EV_DALI_"; ///< prefix to let the SPI tool know it should read the trace
42 }// un-named name space
43
44 KernelTrace::KernelTrace()
45 : mFileDescriptor( 0 ),
46   mLoggedError( false )
47 {
48 }
49
50 KernelTrace::~KernelTrace()
51 {
52   if( mFileDescriptor )
53   {
54     close( mFileDescriptor );
55   }
56 }
57
58 // If this function doesn't appear to work, you can test manually on the device.
59 // $ cd /sys/kernel/debug/tracing
60 //
61 // If the folder doesn't exist then the kernel needs to be re-built with ftrace enabled
62 // If it does exist, then you can continue to test ftrace is working:
63 //
64 // $ echo 1 > tracing_enabled
65 // $ echo "test" > trace_marker
66 // $ cat trace
67 // should print out test message
68 // If the message did not get added to the trace, then check you have write permissions to the trace_marker file.
69 //
70 //
71 void KernelTrace::Trace( const PerformanceMarker& marker, const std::string& traceMessage )
72 {
73   // Open the trace_marker file
74   if( mFileDescriptor == 0 )
75   {
76     mFileDescriptor = open( TRACE_MARKER_FILE , O_WRONLY);
77     if( mFileDescriptor == -1 )
78     {
79       // we want to keep trying to open it, so it will start working if someone fixes
80       // the permissions on the trace marker
81       mFileDescriptor = 0;
82
83       // first time we fail to open the file, log an error
84       if( !mLoggedError )
85       {
86         mLoggedError = true;
87         DALI_LOG_ERROR("Failed to open /sys/kernel/debug/tracing/trace_marker for writing please check file permissions.\n");
88       }
89
90     }
91   }
92
93   if( mFileDescriptor > 0 )
94   {
95       std::string msg( SPI_PREFIX );
96       msg+=traceMessage;
97
98       int ret = write( mFileDescriptor, msg.c_str(), msg.length() );
99           // if it failed then close the file description and try again next time we trace
100       if( ret < 0 )
101       {
102         close( mFileDescriptor );
103         mFileDescriptor = 0;
104       }
105   }
106 }
107
108 } // namespace Internal
109
110 } // namespace Adaptor
111
112 } // namespace Dali
113