Fixes for automated build script:
[synfig.git] / synfig-studio / synfigstudio-cph-monitor
1 #!/bin/bash
2
3 # Synfig Crash Monitor script
4 # Copyright (c) 2009-2010 Konstantin Dmitriev
5 #
6 #       This package is free software; you can redistribute it and/or
7 #       modify it under the terms of the GNU General Public License as
8 #       published by the Free Software Foundation; either version 2 of
9 #       the License, or (at your option) any later version.
10 #
11 #       This package is distributed in the hope that it will be useful,
12 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 #       General Public License for more details.
15
16 set -e
17 trap writelog INT
18
19 init()
20 {
21 echo "SynfigStudio Crash Monitor is a tool to collect statistics about synfig crashes."
22 echo "All information is collected locally in ~/.synfig/cph directory."
23 echo
24 STARTED=0
25 RUNTIME=0
26 VERSION=''
27 RELEASE=''
28 BRANCH=''
29 REVISION_ID=''
30 CRASH=0
31 [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
32
33 # Detect if crash monitor is already started
34 PDIR=${0%`basename $0`}
35 LCK_FILE=~/.synfig/cph/`basename $0`.lck
36 if [ -f "${LCK_FILE}" ]; then
37         MYPID=`head -n 1 "${LCK_FILE}"`
38         if ! ( ps -p ${MYPID} | grep ${MYPID} >/dev/null ); then
39                 # The process is not running
40                 # Echo current PID into lock file
41                 echo $$ > "${LCK_FILE}"
42         else
43                 echo "`basename $0` is already running [${MYPID}]. Aborting."
44                 sleep 5
45                 exit 0
46         fi
47 else
48         # The process is not running
49         # Echo current PID into lock file
50         echo $$ > "${LCK_FILE}"
51 fi
52 echo `date +%H:%M` "Synfig Crash Monitor started."
53 }
54
55 writelog()
56 {
57         if [[ $STARTED != 0 ]]; then
58                 if [[ $CRASH == 0 ]]; then
59                         echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
60                 else
61                         echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
62                 fi
63                 if [ -e ~/.synfig/cph/log ]; then
64                         #check if dump needed
65                         CURRENTDATE=`date +%Y-%m-%d`
66                         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
67                         LOGMODDATE=${LOGMODDATE%% *}
68                         if [[ $LOGMODDATE != $CURRENTDATE ]]; then
69                                 dumpstats
70                         fi
71                 fi
72                 #write log
73                 echo $VERSION/$BRANCH/$RELEASE/$REVISION_ID $RUNTIME $CRASH >> ~/.synfig/cph/log
74                 CRASH=0
75                 RUNTIME=0
76         else
77                 echo
78         fi
79 }
80
81 dumpstats()
82 {
83         echo `date +%H:%M` 'Dumping stats for previous session...'
84         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
85         LOGMODDATE=${LOGMODDATE%% *}
86         #get versions
87         VERSIONS=''
88         while read LINE; do
89                 FOUND=0
90                 for VER in $VERSIONS; do
91                         if [[ $VER == ${LINE%% *} ]]; then
92                                 FOUND=1
93                                 break
94                         fi
95                 done
96                 [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
97         done < ~/.synfig/cph/log
98         echo "   Logged versions: ${VERSIONS}"
99         for VER in $VERSIONS; do
100                 #generating random record ID
101                 ID=$( echo `date` `ps` | md5sum | md5sum )
102                 ID="${ID:2:16}"
103                 #summarizing time and counting crashes
104                 CRASHCOUNT=0
105                 TIMECOUNT=0
106                 while read LINE; do
107                         if [[ ${LINE%% *} == $VER ]]; then
108                                 TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
109                                 CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
110                         fi
111                 done < ~/.synfig/cph/log
112                 echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
113                 echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
114         done
115         rm -f ~/.synfig/cph/log
116         echo '   Done.'
117 }
118
119 mainloop()
120 {
121         while true; do
122                 if ( ps --no-headers -f -Csynfigstudio |egrep "^`whoami`" > /dev/null ) ; then
123                         #synfigstudio process exist
124                         if [[ $STARTED == 0 ]]; then
125                                 STARTED=1
126                                 RUNTIME=0
127                                 #get version
128                                 P=$(ps --no-headers -Csynfigstudio -o uid,cmd |egrep "^ *`id -u`" | egrep -v "<defunct>" | tr -s ' ' | sed "s|^ *`id -u` *||" | sed "s|/bin/sh *||" | head -n 1)
129                                 echo 
130                                 if [ ! -e $P ]; then
131                                         P=`which $P`
132                                 fi
133                                 P=`dirname $P`
134                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
135                                 RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
136                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
137                                 REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
138                                 if [[ $BRANCH == '(no branch)' ]]; then
139                                         BRANCH=$REVISION_ID
140                                 fi
141                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
142                                 echo `date +%H:%M` "Assuming Synfig installed in $P."
143                         else
144                                 let RUNTIME=$RUNTIME+1
145                         fi
146                 else
147                         #no synfigstudio process exist
148                         if [[ $STARTED == 1 ]]; then
149                                 #detect crash
150                                 if [ -e ~/.synfig/fifo ]; then
151                                         CRASH=1
152                                 fi
153                                 writelog
154                                 CRASH=0
155                                 STARTED=0
156                         fi
157                 fi
158                 sleep 1
159         done
160 }
161
162
163 init
164 mainloop