~canonical-dx-team/unity/unity.fix-ql-losing-focus

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
/*
 * Copyright (C) 2009-2010 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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/>.
 *
 * Authored by Gord Allott <gord.allott@canonical.com>
 *
 */

namespace Perf
{
  public class ProcessInfo
  {
    public ProcessInfo (string name)
    {
      this.name = name;
      start = 0;
      end = 0;
    }
    public string name;
    public double start;
    public double end;
  }

  public static TimelineLogger? timeline_singleton;
  public static bool is_logging;

  public class TimelineLogger : Object
  {
    private Timer global_timer;
    private Gee.HashMap<string, ProcessInfo> process_map;

    public static unowned Perf.TimelineLogger get_default ()
    {
      if (Perf.timeline_singleton == null)
      {
        Perf.timeline_singleton = new Perf.TimelineLogger ();
      }

      return Perf.timeline_singleton;
    }

    construct
    {
      this.process_map = new Gee.HashMap<string, ProcessInfo> ();
      this.global_timer = new Timer ();
      this.global_timer.start ();
    }

    public void start_process (string name)
    {
      if (name in this.process_map.keys)
      {
        warning ("already started process: %s", name);
        return;
      }

      var info = new ProcessInfo (name);
      this.process_map[name] = info;
      info.start = this.global_timer.elapsed ();
    }

    public void end_process (string name)
    {
      double end_time = this.global_timer.elapsed ();
      print ("the end time is %f", end_time);

      if (name in this.process_map.keys)
      {
        this.process_map[name].end = end_time;
      }
      else
      {
        warning ("process %s not started", name);
      }
    }

    public void write_log (string filename)
    {
      debug ("Writing performance log file: %s...", filename);
      var log_file = File.new_for_path (filename);
      FileOutputStream file_stream;
      try
      {
        if (!log_file.query_exists (null)) {
          file_stream = log_file.create (FileCreateFlags.NONE, null);
        }
        else
        {
          file_stream = log_file.replace (null, false, FileCreateFlags.NONE, null);
        }

        var output_stream = new DataOutputStream (file_stream);

        foreach (ProcessInfo info in this.process_map.values)
        {
          string name = info.name.replace (",", ";");
          string outline = "%s, %f, %f\n".printf(name, info.start, info.end);
            output_stream.put_string (outline, null);
        }

        file_stream.close (null);
      } catch (Error e)
      {
        warning (e.message);
      }
      debug ("Done writing performance log file: %s", filename);
    }
  }
}