blob: bfdfe8808f46f62937dd2e8cca970606f8993727 [file] [log] [blame]
Chris Lattner1c007c02003-12-31 05:40:02 +00001//===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chris Lattner1c007c02003-12-31 05:40:02 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Chris Lattner1c007c02003-12-31 05:40:02 +00008//===----------------------------------------------------------------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00009//
Chris Lattner1c007c02003-12-31 05:40:02 +000010// This file implements the SlowOperationInformer class for the LLVM debugger.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer7c16caa2004-09-01 22:55:40 +000014#include "llvm/Support/SlowOperationInformer.h"
Reid Spencer61976882005-12-22 03:31:26 +000015#include "llvm/System/Alarm.h"
Chris Lattner1c007c02003-12-31 05:40:02 +000016#include <iostream>
17#include <sstream>
Chris Lattner73546532003-12-31 07:31:10 +000018#include <cassert>
Chris Lattner1c007c02003-12-31 05:40:02 +000019using namespace llvm;
20
Chris Lattner1c007c02003-12-31 05:40:02 +000021SlowOperationInformer::SlowOperationInformer(const std::string &Name)
22 : OperationName(Name), LastPrintAmount(0) {
Reid Spencer61976882005-12-22 03:31:26 +000023 sys::SetupAlarm(1);
Chris Lattner1c007c02003-12-31 05:40:02 +000024}
25
26SlowOperationInformer::~SlowOperationInformer() {
Reid Spencer61976882005-12-22 03:31:26 +000027 sys::TerminateAlarm();
Chris Lattner78f1f512003-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.
31 std::cout << std::string(LastPrintAmount, '\b') << "Progress "
32 << OperationName << ": 100% \n";
33 }
Chris Lattner1c007c02003-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 Lattner5418b0f2006-07-06 22:34:06 +000040bool SlowOperationInformer::progress(unsigned Amount) {
Reid Spencer61976882005-12-22 03:31:26 +000041 int status = sys::AlarmStatus();
42 if (status == -1) {
Chris Lattner1c007c02003-12-31 05:40:02 +000043 std::cout << "\n";
44 LastPrintAmount = 0;
Chris Lattner5418b0f2006-07-06 22:34:06 +000045 return true;
Chris Lattner1c007c02003-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 Spencer61976882005-12-22 03:31:26 +000050 if (status == 0)
Chris Lattner5418b0f2006-07-06 22:34:06 +000051 return false;
Chris Lattner1c007c02003-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 Lattner78f1f512003-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 Lattner1c007c02003-12-31 05:40:02 +000062
63 LastPrintAmount = OS.str().size();
64 std::cout << ToPrint+OS.str() << std::flush;
Chris Lattner5418b0f2006-07-06 22:34:06 +000065 return false;
Chris Lattner1c007c02003-12-31 05:40:02 +000066}