blob: d5ffff9d937fcde152e00f57c7032b38f531f28f [file] [log] [blame]
Chris Lattnera2e93632003-12-31 05:40:02 +00001//===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattnera2e93632003-12-31 05:40:02 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattnera2e93632003-12-31 05:40:02 +00008//===----------------------------------------------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00009//
Chris Lattnera2e93632003-12-31 05:40:02 +000010// This file implements the SlowOperationInformer class for the LLVM debugger.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/SlowOperationInformer.h"
Bill Wendlingfe6b1462006-11-26 10:52:51 +000015#include "llvm/Support/Streams.h"
Reid Spencera6d50d42005-12-22 03:31:26 +000016#include "llvm/System/Alarm.h"
Chris Lattnera2e93632003-12-31 05:40:02 +000017#include <sstream>
Chris Lattner0a5f0332003-12-31 07:31:10 +000018#include <cassert>
Chris Lattnera2e93632003-12-31 05:40:02 +000019using namespace llvm;
20
Chris Lattnera2e93632003-12-31 05:40:02 +000021SlowOperationInformer::SlowOperationInformer(const std::string &Name)
22 : OperationName(Name), LastPrintAmount(0) {
Reid Spencera6d50d42005-12-22 03:31:26 +000023 sys::SetupAlarm(1);
Chris Lattnera2e93632003-12-31 05:40:02 +000024}
25
26SlowOperationInformer::~SlowOperationInformer() {
Reid Spencera6d50d42005-12-22 03:31:26 +000027 sys::TerminateAlarm();
Chris Lattner9e260272003-12-31 10:20:38 +000028 if (LastPrintAmount) {
29 // If we have printed something, make _sure_ we print the 100% amount, and
30 // also print a newline.
Bill Wendlinge8156192006-12-07 01:30:32 +000031 cout << std::string(LastPrintAmount, '\b') << "Progress "
32 << OperationName << ": 100% \n";
Chris Lattner9e260272003-12-31 10:20:38 +000033 }
Chris Lattnera2e93632003-12-31 05:40:02 +000034}
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).
Chris Lattner148f4402006-07-06 22:34:06 +000040bool SlowOperationInformer::progress(unsigned Amount) {
Reid Spencera6d50d42005-12-22 03:31:26 +000041 int status = sys::AlarmStatus();
42 if (status == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000043 cout << "\n";
Chris Lattnera2e93632003-12-31 05:40:02 +000044 LastPrintAmount = 0;
Chris Lattner148f4402006-07-06 22:34:06 +000045 return true;
Chris Lattnera2e93632003-12-31 05:40:02 +000046 }
47
48 // If we haven't spent enough time in this operation to warrant displaying the
49 // progress bar, don't do so yet.
Reid Spencera6d50d42005-12-22 03:31:26 +000050 if (status == 0)
Chris Lattner148f4402006-07-06 22:34:06 +000051 return false;
Chris Lattnera2e93632003-12-31 05:40:02 +000052
53 // Delete whatever we printed last time.
54 std::string ToPrint = std::string(LastPrintAmount, '\b');
55
56 std::ostringstream OS;
Chris Lattner9e260272003-12-31 10:20:38 +000057 OS << "Progress " << OperationName << ": " << Amount/10;
58 if (unsigned Rem = Amount % 10)
59 OS << "." << Rem << "%";
60 else
61 OS << "% ";
Chris Lattnera2e93632003-12-31 05:40:02 +000062
63 LastPrintAmount = OS.str().size();
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cout << ToPrint+OS.str() << std::flush;
Chris Lattner148f4402006-07-06 22:34:06 +000065 return false;
Chris Lattnera2e93632003-12-31 05:40:02 +000066}