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