blob: b4e9430e5fdfeea8555e1165a15e4b847280c636 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SlowOperationInformer class for the LLVM debugger.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/SlowOperationInformer.h"
Chris Lattner5febcae2009-08-23 08:43:55 +000015#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/System/Alarm.h"
17#include <sstream>
18#include <cassert>
19using namespace llvm;
20
21SlowOperationInformer::SlowOperationInformer(const std::string &Name)
22 : OperationName(Name), LastPrintAmount(0) {
23 sys::SetupAlarm(1);
24}
25
26SlowOperationInformer::~SlowOperationInformer() {
27 sys::TerminateAlarm();
28 if (LastPrintAmount) {
29 // If we have printed something, make _sure_ we print the 100% amount, and
30 // also print a newline.
Chris Lattner5febcae2009-08-23 08:43:55 +000031 outs() << std::string(LastPrintAmount, '\b') << "Progress "
32 << OperationName << ": 100% \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 }
34}
35
36/// progress - Clients should periodically call this method when they are in
37/// an exception-safe state. The Amount variable should indicate how far
38/// along the operation is, given in 1/10ths of a percent (in other words,
39/// Amount should range from 0 to 1000).
40bool SlowOperationInformer::progress(unsigned Amount) {
41 int status = sys::AlarmStatus();
42 if (status == -1) {
Chris Lattner5febcae2009-08-23 08:43:55 +000043 outs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 LastPrintAmount = 0;
45 return true;
46 }
47
48 // If we haven't spent enough time in this operation to warrant displaying the
49 // progress bar, don't do so yet.
50 if (status == 0)
51 return false;
52
53 // Delete whatever we printed last time.
54 std::string ToPrint = std::string(LastPrintAmount, '\b');
55
56 std::ostringstream OS;
57 OS << "Progress " << OperationName << ": " << Amount/10;
58 if (unsigned Rem = Amount % 10)
59 OS << "." << Rem << "%";
60 else
61 OS << "% ";
62
63 LastPrintAmount = OS.str().size();
Chris Lattner5febcae2009-08-23 08:43:55 +000064 outs() << ToPrint+OS.str();
65 outs().flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return false;
67}