Synfig Studio Crash Monitor.
[synfig.git] / synfig-studio / trunk / 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 `date +%H:%M` "Synfig Crash Monitor started."
21 STARTED=0
22 RUNTIME=0
23 VERSION=''
24 RELEASE=''
25 BRANCH=''
26 CRASH=0
27 [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
28 }
29
30 writelog()
31 {
32         if [[ $STARTED != 0 ]]; then
33                 if [[ $CRASH == 0 ]]; then
34                         echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
35                 else
36                         echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
37                 fi
38                 if [ -e ~/.synfig/cph/log ]; then
39                         #check if dump needed
40                         CURRENTDATE=`date +%Y-%m-%d`
41                         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
42                         LOGMODDATE=${LOGMODDATE%% *}
43                         if [[ $LOGMODDATE != $CURRENTDATE ]]; then
44                                 dumpstats
45                         fi
46                 fi
47                 #write log
48                 echo $VERSION/$BRANCH/$RELEASE $RUNTIME $CRASH >> ~/.synfig/cph/log
49                 CRASH=0
50                 RUNTIME=0
51         else
52                 echo
53         fi
54 }
55
56 dumpstats()
57 {
58         echo `date +%H:%M` 'Dumping stats for previous session...'
59         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
60         LOGMODDATE=${LOGMODDATE%% *}
61         #get versions
62         VERSIONS=''
63         while read LINE; do
64                 FOUND=0
65                 for VER in $VERSIONS; do
66                         if [[ $VER == ${LINE%% *} ]]; then
67                                 FOUND=1
68                                 break
69                         fi
70                 done
71                 [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
72         done < ~/.synfig/cph/log
73         echo "   Logged versions: ${VERSIONS}"
74         for VER in $VERSIONS; do
75                 #generating random record ID
76                 ID=$( echo `date` `ps` | md5sum | md5sum )
77                 ID="${ID:2:16}"
78                 #summarizing time and counting crashes
79                 CRASHCOUNT=0
80                 TIMECOUNT=0
81                 while read LINE; do
82                         if [[ ${LINE%% *} == $VER ]]; then
83                                 TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
84                                 CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
85                         fi
86                 done < ~/.synfig/cph/log
87                 echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
88                 echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
89         done
90         rm -f ~/.synfig/cph/log
91         echo '   Done.'
92 }
93
94 mainloop()
95 {
96         while true; do
97                 if ( ps -f -u `whoami`|egrep "synfigstudio$" >/dev/null ) ; then
98                         #synfigstudio process exist
99                         if [[ $STARTED == 0 ]]; then
100                                 STARTED=1
101                                 RUNTIME=0
102                                 #get version
103                                 P=$(ps -f -u `whoami`|egrep "synfigstudio$"| tr -s ' '| cut -d ' ' -f 8)
104                                 echo 
105                                 if [ ! -e $P ]; then
106                                         P=`which $P`
107                                 fi
108                                 echo $P
109                                 P=`dirname $P`
110                                 echo "   Assuming synfig installed in $P."
111                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
112                                 RELEASE=`$P/synfig --info|egrep "Development version:"|cut -d ' ' -f 3`
113                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2`
114                                 if [[ $BRANCH == '(no branch)' ]]; then
115                                         BRANCH=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
116                                 fi
117                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH started."
118                         else
119                                 let RUNTIME=$RUNTIME+1
120                         fi
121                 else
122                         #no synfigstudio process exist
123                         if [[ $STARTED == 1 ]]; then
124                                 #detect crash
125                                 if [ -e ~/.synfig/fifo ]; then
126                                         CRASH=1
127                                 fi
128                                 writelog
129                                 CRASH=0
130                                 STARTED=0
131                         fi
132                 fi
133                 sleep 1
134         done
135 }
136
137 init
138 mainloop